To Delete user profiles with PowerShell you can use Remove-CIMInstance. But with great power comes great responsibility..So lets see how we can manage profiles.
Get-CimInstance -Class Win32_UserProfile |ft
Will return some useful information but not all we need to know
It shows me:
- the last time a profile was used (LastUseTime)
- The path for the user profile (LocalPath)
- if the profile is loaded (Loaded) – meaning a user is logged in or a service is using a profile using
- If it is a roaming profile, the path to the roaming profile and the roaming preference
- the SID of the user.
That does not tell me everything, so lets look at just 1 user, UserAdmin
Get-CimInstance -Class Win32_UserProfile |where-object -property sid -eq S-1-5-21-344710886-3474993480-1676603463-1002
Oh look there is now a property called “Special”
Hmm what does that mean?
Get-CimInstance -Class Win32_UserProfile | select-object LastUseTime,loaded,localpath,special,sid |ft
Ah I see , Special, looks like it is a “special account”, so probably some user profiles we do not want to mess with or the world may end.
Also messing with something that is “Loaded” is probably not good either as, that is either a user logged into the PC or a service.
Ok so lets filter things out
Get-CimInstance -Class Win32_UserProfile | Where-Object { ($_.Special -eq $False) -and ($_.Loaded -eq $False) }|ft
Ok that cool, I not have all profiles that are not loaded and are not special… but that one looks special…. ok hang on I will create a user
hmm that’s interesting
I have users with no profiles and they are disabled… anyhow
Ok I run the command above and BritV8 does not have a profile… of course they don’t they have never logged in! I can fix that!
Ok so a quick check and their profile directory is created
Lets check the registry
Yep BritV8 is there too