Blog
By Tommy Gunn on 8/10/2009 10:46:49 PM • Rank (4073) • Views 4349
0

0
powershell2xa4 When BING-ing the Internet (if Google can be used as a verb, shouldn’t BING?)…Anyway, I was searching the Internet for a way to recursively delete a group of subdirectories and files which are left behind by our backup software every night.

While I found parts of the solution I wanted (delete directories, delete files), I could not find quite what I wanted (and in some cases, samples did not work as expected). I needed to find the Powershell syntax that when given a target directory would do all of the following at once:

  • Delete all subdirectories (recursive deletion)
  • Delete all files in this directory and subdirectories
  • Require no user input
  • Delete read-only files as well

With a bit of searching and some testing, here’s what I found that may come in handy for some of you. BTW, I am using Powershell v1 here. And another BTW, I am thinking about using this as part of an agent task in OpsMgr, for when I’d like to clean this mess up on demand rather than on a schedule. Although if you wanted to, you could use this in a timed script rule to do some scheduled cleanup work.

Retrieving the Files and Folders

Get-childitem can be used to retrieve files and folders, and it has three parameters that are useful in this situation

-include, –exclude and -recurse – In short, you can use wildcards to specify what should be included and excluded in the retrieval, and then with –recurse, instruct it to go recursively through all subdirectories. In my case, I wanted to retrieve everything, and in testing found

get-childitem "c:\temp2" -include * -recurse

And Now, How to Delete Them

Once we’ve retrieved the files and folder, now we can pass them to another cmdlet through the pipeline) – remove-item. This cmdlet has two parameters that were also critical: –recurse and –force. The first tells remove-item to delete all objects down the tree (recursively) and the –force parameter is what allows deletion of read-only files…and also eliminates an annoying prompt for approval.

get-childitem "c:\temp2" -include * -recurse | Remove-Item -recurse –force

But First, Testing the Code

I was pretty sure this would work, but it’s always nice to check first (measure twice, cut once, as grandpa used to say). This is where the –whatif parameter comes into play. Tack this onto the end of the one-liner above, and the results of actions that would be taken if we ran this for real are echoed to the screen with no harm done.

get-childitem "c:\temp2" -include * -recurse | Remove-Item -recurse –forcewhatif

Conclusion

This only took a few minutes, but with review of some samples on the Internet and the get-help cmdlet, I was able to get-r-done!

get-childitem "c:\temp2" -include * -recurse | Remove-Item -recurse –force

Comments - Comment RSS


Who Viewed
Who Reviewed
Categories
Related Pages
Shortened URL
http://tinyurl.com/lrhkeq

Top Contributors
Featured Members
Pete Zerger
Points: 65622
Level: System Center Expert
Tommy Gunn
Points: 42748
Level: System Center Expert
Simon Skinner
Points: 40804
Level: System Center Expert
Stefan Koell
Points: 28999
Level: System Center Expert
Andreas Zuckerhut
Points: 27734
Level: System Center Expert