Skip to content

Instantly share code, notes, and snippets.

@PSingletary
Created March 1, 2020 00:34
Show Gist options
  • Select an option

  • Save PSingletary/26a5f2413036d03f8491c0812e913427 to your computer and use it in GitHub Desktop.

Select an option

Save PSingletary/26a5f2413036d03f8491c0812e913427 to your computer and use it in GitHub Desktop.
Invoke-Command -ComputerName $WBComputers.Name -ScriptBlock {
$GoneUsers = Get-WmiObject -Class win32_userprofile |
Select-Object -Property *, @{
NAME = 'user'
EXPRESSION = {
((New-Object -TypeName System.Security.Principal.SecurityIdentifier -ArgumentList ($_.SID)).Translate([System.Security.Principal.NTAccount])).Value
}
} |
Where-Object -FilterScript {
$_.User -eq $null
}
$GoneUsers | ForEach-Object -Process {
if ($_.LocalPath)
{
$prof = Get-WmiObject -Class win32_userprofile -Filter "SID='$($_.SID)'"
$prof.delete()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment