Script to remove files/folders from all local profiles

powers

I had a requirement recently that required a folder and all files under it to be removed from all local user profiles on a machine – turns out this is really simple to do in powershell:


# Get a list of all local profiles on the target machine # 
$users = Get-ChildItem c:\users


# Now cycle through each profile.. #


foreach ($user in $users)


# Delete the folder and everything under it.. #


$folder = "C:\users\" + $user +"\yourfoldernamehere"
Remove-Item $folder -Recurse -Force -ErrorAction SilentlyContinue


# If you also want to delete it from the user's network profile.. #

$netfolder = "\\servername\profileroot$\" + $user  +"\yourfoldernamehere"
Remove-Item $netfolder -Recurse -Force -ErrorAction SilentlyContinue

obviously, “yourfoldername” is the name of the folder you want to delete, and “\\servername\profileroot$” is the path to your users’ network profiles.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.