Skip to main content
Question

Deleting a local user account


Forum|alt.badge.img

We are trying to remove an old local account that is on most of our windows computers.  The code removed the account when I ran it locally but not when I run it through automox.

 

# Delete a local account named "tommyboy" 
$AccountName = "tommyboy"

# Check if the account exists
if (Get-LocalUser -Name $AccountName -ErrorAction SilentlyContinue) {
    # Forcefully delete the account
    Remove-LocalUser -Name $AccountName -Force
    Write-Host "The account '$AccountName' has been deleted successfully."
} else {
    Write-Host "The account '$AccountName' does not exist."
}

 

2 replies

Forum|alt.badge.img
  • Automox Employee
  • 134 replies
  • April 3, 2025

Hi Slammert,

This is a situation where get-localuser is not available for 32-bit powershell

 

 

I was able to get this pieced together which worked, using 32-bit available commands:

 

# ======================
# Otto AI Generated Code
# ======================
# Define the username of the account you want to delete
$username = "delete-me"  # Replace with the actual username

# Check if the user exists using net user command
$userExists = net user | Select-String -Pattern $username

if ($userExists) {
    # Remove the user account using net user command
    net user $username /delete
    Write-Host "User account '$username' has been deleted."
} else {
    Write-Host "User account '$username' does not exist."
}

 


Forum|alt.badge.img
  • Author
  • Novice
  • 20 replies
  • April 3, 2025

Thanks Mark!!!

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings