PowerShell script makes it easy to find and delete them
As the result of a few wrong switches, Microsoft Systems Management Server (SMS) created but didn't delete some folders on many computers at my company. For example, the folders highlighted in Figure 1 were some of the folders added to one machine.
Figure 1: Example of folders created but not deleted by SMS
Because the folders created by SMS didn't have Full Control Administrators permission applied, deleting those folders involved:
- Logging on to each machine locally or remotely.
- Applying Full Control Administrators permission to each folder. Without this permission, the folders can't be deleted by administrators.
- Deleting the folders.
Manually performing these steps would've been time-consuming, so I wrote a PowerShell script, deljunkfolders.ps1, to automatically delete the folders and their contents. All the folders contained a subfolder named update, like that in Figure 2. So, deljunkfolders.ps1 looks for subfolders named update in top-level directories.
Figure 2: Looking for an update subfolder in top-level folders
After finding all the top-level folders that contain an update subfolder, deljunkfolders.ps1 uses the Get-Acl cmdlet to copy permissions from a folder where the administrators have Full Control Administrators permission, then uses the Set-Acl cmdlet to apply that permission to the folder that needs to be deleted. Finally, the script either displays the folders to be deleted or deletes them, depending on the command you use to launch the script.
If you want to preview the folders that will be deleted and deljunkfolders.ps1 resides on the D drive, you'd use a command such as
Powershell.exe D:\deljunkfolders.ps1 Pclist.txt
Pclist.txt is an input file that contains the names of the computers you want to check. When you create this text file, the computer names need to follow format
If you're happy with the results in the preview, you can perform the actual deletion using a command such as
Powershell.exe D:\deljunkfolders.ps1 Pclist.txt 1
The last argument (1) tells the script to delete the folders. Note that if a computer is unavailable, the script returns a message stating that the ping failed for that computer.
You can download deljunkfolders.ps1 by clicking the Download the Code Here button at the top of the page. Using this script as a template, you can create your own solution to delete folders that SMS created but didn't delete. I used PowerShell 1.0 to create this script, which I tested on Windows XP.