Forum Discussion

ruisampaio's avatar
ruisampaio
Copper Contributor
Feb 21, 2024

Intune Powershell Script to delete Folders

Hi,

I'm no expert in Powershell, and I'm willing to delete folders on computers that are managed by Intune.

 

I already tried some scripts in Powershell to run in Intune, but I have no success in deleting the folders.

 

How should I do it?

 

Folder is located:

c:\Users\STUDENTNAME\Appdata\Local\FOLDERtoDELETE

 

and

 

c:\Users\STUDENTNAME\Appdata\Roaming\FOLDERtoDELETE

 

 

 

I've tried this script in Platform scripts:

 

$foldersToCheck = @(

"${env:LOCALAppData}\FOLDERtoDELETE_1",

"${env:AppData}\FOLDERtoDELETE_2",

"${env:ProgramFiles}\FOLDERtoDELETE_3",

"${env:ProgramFiles(x86)}\FOLDERtoDELETE_4"

)



foreach ($folderPath in $foldersToCheck) {

    if (Test-Path $folderPath -PathType Container) {

        try {

            Remove-Item -Path $folderPath -Recurse -Force -ErrorAction Stop

            Write-Host "Folder '$folderPath' has been deleted."

        } catch {

            Write-Host "Error deleting folder '$folderPath': $_"

        }

    } else {

        Write-Host "Folder '$folderPath' does not exist."

    }

}

 

 

 

Thank you very much in advance!

    • ruisampaio's avatar
      ruisampaio
      Copper Contributor
      Do you mean "Run this script using the logged on credentials"?
      It's set as "No", should it be set as "Yes"?
      • Harm_Veenstra's avatar
        Harm_Veenstra
        MVP

        ruisampaio It should be Yes. Because you want to remove folders in the appdata folder of the user (And not from System)

         

        Please click Mark as Best Response & Like if my post helped you to solve your issue.
        This will help others to find the correct solution easily. It also closes the item.

        If one of the posts was helpful in other ways, please consider giving it a Like.

Resources