开发者

powershell script to remove multiple folders

开发者 https://www.devze.com 2023-04-08 18:42 出处:网络
I would like a script to remove multiple folders from beneath each users douments and settings. Example 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 settings

I would like it to delete everythi开发者_JAVA百科ng except the following.

my documents

mmerge

desktop

favorites

Is 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号