Remove Local User Account in Windows

When using a worklet to remove a local user account in 64-bit Windows, you will need to wrap the script in a scriptblock, as the module for LocalAccounts isn't available for 32-bit PowerShell.

 

$scriptblock = {
Import-Module Microsoft.PowerShell.LocalAccounts
Import-Module Microsoft.PowerShell.Logging
 
$username = 'localaccount'
 
if (Get-LocalUser -Name $username -ErrorAction SilentlyContinue) {
  Write-Log "Attempting to remove local user account $username"
  Remove-LocalUser $username
  Write-Log "Successfully removed local user account $username"

else {
  Write-Error "The local user account $username does not exist"
}
}
& "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptblock
Was this article helpful?
0 out of 0 found this helpful