PowerShell: delete old Windows user profiles safely
The script
Enumerate Win32_UserProfile, filter out special and loaded profiles, and remove the ones unused for 90 days. The script below runs with -WhatIf so the first run only prints what it would delete; remove that switch once the list looks right.
$cutoff = (Get-Date).AddDays(-90)
Get-CimInstance Win32_UserProfile |
Where-Object { -not $_.Special -and -not $_.Loaded -and
$_.LastUseTime -and $_.LastUseTime -lt $cutoff } |
ForEach-Object {
"{0}`t{1:d}" -f $_.LocalPath, $_.LastUseTime
Remove-CimInstance -InputObject $_ -WhatIf # remove -WhatIf when happy
}
Notes before you run it
- Removing via
Win32_UserProfiledeletes both the folder and the registry entry. Never deleteC:\Users\nameby hand; that leaves a broken profile that gives the returning user a temporary one. LastUseTimehas been unreliable on some Windows 10 builds, where maintenance tasks touch it. Sanity-check the printed list against what you know before removing the switch.- Roaming or synced profiles: deleting the local copy is safe by design, but confirm before assuming.
Run it on every machine, not one at a time.
Save any of these as a command in Moorfox and run it on a machine in one click, output and exit code captured, with no PSRemoting or WinRM setup. The agent runs it locally and reports back, even over the internet.