powershell script to remove multiple folders
I would like a script to remove multiple folders from beneath each users douments and settings.
Example folders:
Desktop
Favorites mmerge my documents nethood printhood recent sentto start menu windows application data local settingsI would like it to delete everythi开发者_JAVA百科ng except the following.
my documents
mmerge desktop favoritesIs there a way to scropt this or remove all with an exception list perhaps?
This is the same for all users.path e:\redirected\%username%\
You probably need to employ the Remove-Item cmdlet but before doing so, you seriously need to review your requirements. For example, removing all folders under local setting or application data is likely to break stuff (i.e. installed applications). You'll also need to test for file locks and define a strategy for dealing with files and folders that can't be deleted whilst the user is logged in.
Try something like:
ls '\Documents and Settings' | ? { ! ($_.psiscontainer -and $_ -match "my documents|mmerge|desktop|favorites") } | rm -rec -force -whatif
Obviously you need to be very careful with the rm -rec
and remove the -whatif. ;) Add -force to the ls
(Get-ChildItem) if you want to make hidden folders visible to your destruction.
精彩评论