Powershell to delete old local profiles..help!
As a requirement to clear disk space on some shared pcs where users are not meant to save data on , I need to delete local profiles older than 30 days.
I have put together the following script but keep getting
" remove-item : Access to the path "C:\Users\user1\AppData\Local\Application Data is denied"
this is strange because am running script with Admin priviledge and can manually delete files/folders in that directory. Can anyone shed some light on this? Any suggestion will be highly appreciated.
get-childitem "c:\users\" -exclude "Admin*", "All Users", "TEMP" ,"public", "Default" , "Default User" |? {$_.psiscontainer -and $_.lastwritetime -le (get-date).adddays(-30)} |% {remove-item $_ -Force}
1 Comment
[ + ] Show comment
-
The delprof tool does not work on newer versions of the Windows OS. There is another version delprof2 that does and you can set it up as a scheduled task with the t30 argument. It's free to educational groups. https://helgeklein.com/free-tools/delprof2-user-profile-deletion-tool/ - Electro 8 years ago
Answers (4)
Please log in to answer
Posted by:
rockhead44
9 years ago
Posted by:
rileyz
9 years ago
Try this.
You'll need a way to get the accounts on the machine, then pass them into a array so it can cycle through them. But the call below will use, you guess it, WMI to remove the profile. It works for local profile, not sure about Domain user accounts tho. Btw, careful when you delete profiles like the way your are doing it, there some other cleanup actions going on in the os background, sometimes you will end up with useracc.001 useracc.002 when they log back on (which turns into a profile nightmare)
$UserObj = Get-WmiObject Win32_UserProfile -filter "localpath='C:\\Users\\$LocalAccountName'"
$UserObj.Delete()
Posted by:
TonyFishers
9 years ago