r/PowerShell • • 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 -WhatIf

How do you handle this? GPO "delete profiles older than X days", DelProf2, or something custom?

0 Upvotes

26 comments sorted by

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?

3

u/BlackV 27d ago

it looks at the same stale/inaccurate date that other tools look at

-2

u/ProfileSweep 27d ago

That works if the profile age data is reliable, but as BlackV said, the GPO uses the same timestamps that get bumped by AV scans and services, so profiles can look "recent" and never get removed. It also deletes without a backup. For loaner laptops I wanted a backup first and a cleanup right when the laptop comes back, not after X days, so I went with a manual run instead.

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

  1. this is not a gottcha in the slighest, its always been this way, since win95/nt
  2. lastusetime does not "lie", but they are looking at shitty data, shitty data they have not fixed in 10+ something years :(
  3. robocopy exit codes 0-7 is success with caveats (I also thought it was more than just 0-7)
  4. It is obvious
  5. Two profile records can point at the same folder, I have never seen this, and I'm not sure I believe you, did you actually test or just look at the folder name
  6. would it? would it have wiped the admin folder? seeing as they are logged in and the profile is in use
  7. how do I handle it question, both delprof2 and gpo look at the same value

-4

u/Incrediblecodeman 27d ago

Don’t be a nerd he’s a cool person sharing a cool idea.

6

u/BlackV 27d ago edited 27d ago

Are they? Where ?, only code is get ciminstance and remove ciminstance , but that's already known

Op does in their profile just happen to be selling a profile tool (not linked here) for real $$$

That implies a lack of authenticity in their post, I would rather look at code and give feedback to that

-2

u/Incrediblecodeman 27d ago

Shitty data “they” didn’t fix? No dude the coder who did it moved on. Probably dead now. You really think Microsoft hires people to go read existing code of insignificance ? Maybe now with ai people can but you just harmed a potential future candidate. God knows why you didn’t go to Microsoft to personally “fix the shitty data” all by your self. Ya just use Linux huh?

3

u/BlackV 27d ago

Ya it's probably the exact same code from nt4, not touched in a million years

-3

u/Incrediblecodeman 27d ago

I googled it and it doesn’t look it he’s selling a tool for money.

2

u/BlackV 27d ago

Did you happen to look at their profile?

-3

u/Incrediblecodeman 27d ago

Don’t feel guilty. He’s in a different stage of like then you. He is starting growing learning having fun and trying new things. You seem to have it all figured out. Methodical, more mundane even. Less willing to even try to have a spark about anything in particular.

Please don’t be snarky to young people just because you are older and had more time to do the crafts. Dosnt make you “better” your amongst genius people and their moods are fragile to passive aggressive light online bullying like your comment

2

u/BlackV 27d ago

You don't feel guilty?

-4

u/ProfileSweep 27d ago

Fair criticism, thanks for taking the time. Going through it:

1–4: Agreed, “gotchas” was oversold. These are well known to anyone who’s done this for a while.

5: I did see it. Output from my test machine:

SID                                          Loaded  Account
S-1-5-21-...-1004                            True    PC\mmcadmin
S-1-5-21-...-1001                            False   <orphaned>

Both with LocalPath = C:\Users\mmcadmin. The account was deleted and recreated, and the new one ended up on the old folder. As someone pointed out on another forum, that was probably a manual ProfileImagePath repoint, which is the real mistake.

6: You’re right to push on this. I only ran it with -WhatIf. I didn’t run the actual delete against a logged-in profile, so “would have wiped” was overstated. My expectation is a partial delete (unlocked files go, locked ones like NTUSER.DAT stay), but I haven’t tested it, so I shouldn’t have stated it as fact.

7: Fair. Same underlying data.

Lesson taken on testing before claiming. Appreciate the pushback.

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

0

u/Incrediblecodeman 27d ago

And before you mean nerds talk shit yes I know there’s a SID but it’s still cool for science. Don’t rain on anyone parade

2

u/[deleted] 27d ago

[removed] — view removed comment

0

u/Incrediblecodeman 27d ago

I download that and now 2 men in suits are here and saying I have to come with them

1

u/[deleted] 27d ago

[removed] — view removed comment

1

u/Barious_01 27d ago

and don't stub yourself with a call with a single preperty. Shit man. with the .value does not pipe to remove cim. You have picked up just a string property and you expect powereshell to pick up the rest pf the object not how it work my dude. Get away from piping simple strings and expect an entire object from it. For hell sake. Yes someone did piss in my chreose. However, this is easier why you use get member and break down what the pipe is held.