Advertisement
posLop

ps2

Sep 18th, 2024 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #This script deletes profiles older than DaysOld
  2.  
  3. #The list of accounts, which profiles must not be deleted
  4. $ExcludedUsers = "Public","Default”,"tester1"
  5.  
  6. #How many days old the profile must be to be removed
  7. $DaysOld = 7
  8.  
  9. $LocalProfiles=Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and (!$_.Loaded) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-$DaysOld))}
  10. foreach ($LocalProfile in $LocalProfiles)
  11. {
  12. if (!($ExcludedUsers -like $LocalProfile.LocalPath.Replace("C:\Users\","")))
  13. {
  14. $LocalProfile | Remove-WmiObject
  15. Write-host $LocalProfile.LocalPath, "profile deleted” -ForegroundColor Magenta
  16. }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement