Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #This script deletes profiles older than DaysOld
- #The list of accounts, which profiles must not be deleted
- $ExcludedUsers = "Public","Default”,"tester1"
- #How many days old the profile must be to be removed
- $DaysOld = 7
- $LocalProfiles=Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and (!$_.Loaded) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-$DaysOld))}
- foreach ($LocalProfile in $LocalProfiles)
- {
- if (!($ExcludedUsers -like $LocalProfile.LocalPath.Replace("C:\Users\","")))
- {
- $LocalProfile | Remove-WmiObject
- Write-host $LocalProfile.LocalPath, "profile deleted” -ForegroundColor Magenta
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement