r/PowerShell • u/ProfileSweep • 28d ago
AI Disclosure Cleaning up old user profiles on shared PCs: 5 gotchas I hit (orphaned SIDs can wipe a live user's folder)
AI disclosure: I used an AI assistant (Claude) to help review my script and tidy up this post. The testing and findings are from my own machines.
I look after a pool of loaner laptops and meeting-room PCs. Each machine ends up with dozens of stale profiles, and disks fill up fast. Here's what I learned automating the cleanup:
1. Don't just delete C:\Users\<name>. The registry entry under ProfileList stays behind, and the next login gets a temporary profile. Remove the profile properly through Win32_UserProfile.
2. LastUseTime lies. AV scans and services touch profiles and update it. I even saw it update on a profile whose account no longer exists.
3. Check Robocopy exit codes. 0-7 count as success, and 8 or above means failure. Verify the backup before you delete anything.
4. Skip loaded profiles, special accounts and your own admin account. Sounds obvious until a scheduled task runs while someone is logged in.
5. Two profile records can point at the same folder. On my test machine, a deleted-and-recreated account left an orphaned SID still pointing at C:\Users\<admin>, the same folder the logged-in admin was using. A naive "remove every unloaded profile" would have wiped the live admin's folder. Now I skip any record whose LocalPath is shared with a loaded profile:
$profiles = Get-CimInstance Win32_UserProfile | Where-Object { -not $_.Special -and $_.LocalPath -like 'C:\Users\*' }
$loadedPaths = ($profiles | Where-Object Loaded).LocalPath
$profiles | Where-Object { -not $_.Loaded -and $_.LocalPath -notin $loadedPaths } |
Remove-CimInstance -WhatIfHow do you handle this? GPO "delete profiles older than X days", DelProf2, or something custom?
1
u/TechnicaVivunt 27d ago
Is there a reason to not just use the gpo to delete the profile after x amount of days + scheduled task to reboot?
6
u/BlackV 27d ago edited 27d ago
.......
you and your AI didn't even post the script.....
did your AI proofread the post , there seems to be quite a lack of details or needs giant footnotes
2
u/Pisnaz 27d ago
This info is not new, anyone wiping profiles since windows 10 should have been tracking this.
I cobbled up a script ages ago to solve this, after verifying my admins and any logged in users to purge, properly, the profile. We can remotely execute it on any device with a system name and have unique IDs for systems that just never load profiles for high user usage scenarios.
1
u/Overall-Ad4796 25d ago
„If your script or content is suspected of being AI written and it is not disclosed in a tag or at the top of the post, it will be removed until investigated.”
you did neither. please tag it so its easier to skip your slop without having to open it first
0
u/[deleted] 27d ago
[deleted] — view deleted comment
2
u/[deleted] 27d ago
[removed] — view removed comment
1
u/[deleted] 27d ago
[removed] — view removed comment