Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

August 06, 2009 12:00 AM

Delete Junk Folders Created by SMS

PowerShell script makes it easy to find and delete them
Windows IT Pro
InstantDoc ID #102279
Rating: (1)
Downloads
102279.zip

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

Pc001
Pc002
Pc003

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.

Related Content:

ARTICLE TOOLS

Comments
  • ToRise...
    2 years ago
    Nov 03, 2010

    I have tried to run this script to remove folders of the same type mentioned in this article unsuccessfully. One many of the systems in my shop have the long encrypted file names but in subdirectories are i386 and amd64. In the script I tried to change "update" to make one of the other search criteria with no luck. I even added an update folder to one of the machines at question. The script returns 0 to be deleted. Not sure what I am doing wrong.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.