I want to remove local user in windows 10 computer through powershell.
I have tried command
Get-WMIObject -class Win32_UserProfile | Where {((!$_.Sp
@( Get-WMIObject -class 'Win32_UserProfile' -ComputerName 'Terminal001.domain.local' )|
Where-Object { <**some filter conditions**> } |
Where-Object { $_.Special -eq $false } |
Where-Object { $_.Loaded -eq $false } |
Where-Object { $_.Status -eq 2 } | # We remove only local copies of Roaming profiles. We assume there is a copy of Roaming profile on a server.
Foreach-Object {
$local:profile = $_;
Write-Host -f "Gray" "Deleting $($_.LocalPath)`t" -noNewLine
try {
$local:profile.Delete()
Write-Host "OK!" -f Green
} catch {
Write-Host -f "Yellow" "Unable to delete $($local:profile.LoalPath) : $($_.Exception.Message)"
}
}
For Status see docs for Win32_UserProfile class
Please take a note, that LocalPath is not always equal username. For example, if username changed while profile is created already, LocalPath does not change. Or, else, profile folder can be named not Username, but Username.Userdomain, or UserName.UserDomain.nnn (nnn - number).
Better way is to compare SIDs:
$userSID = (Get-ADUser 'username').SID
Get-WmiObject -Class 'Win32_UserProfile' .... |
Where-Object {$_.SID -eq $userSID} |
...