I don't remember where I got this, but here is a Powershell script that should remove the profiles of deleted user accounts. on a PC
It translates a SID to a user. The -whatif just shows the command to be executed and doesn't actually perform the action.
Get-CimInstance win32_userprofile | foreach {
""
$u = $_ # save our user to delete later
try {
$objSID = New-Object System.Security.Principal.SecurityIdentifier($u.sid) -ErrorAction stop
$objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
"User={0}" -f $objUser.Value
"Path={0}" -f $u.LocalPath
"SID={0}" -f $u.SID
}
catch {
"!!!!!Account Unknown!!!!!"
"Path={0}" -f $u.LocalPath
"SID={0}" -f $u.SID
Remove-CimInstance -inputobject $u -Verbose -WhatIf
}
}
There is also PsGetSid
https://learn.microsoft.com/en-us/sysinternals/downloads/psgetsid