Remove/Disable Local Accounts Except Administrator in Powershell

Gustavo Lorenzett 126 Reputation points
2022-10-08T23:39:34.013+00:00

Guys, I need help.

Which powershell command do I use to remove/disable all local accounts (Windows 10). Leaving only the Administrator account active.

Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-10-08T23:52:44.213+00:00

    Hi @Gustavo Lorenzett ,

    you can try this:

    # Disable all local users with name not equal "Administrator"  
    Get-LocalUser | Where-Object {$_.Name -ne "Administrator"} | Disable-LocalUser -WhatIf  
    # Delete all local users with name not equal "Administrator"  
    Get-LocalUser | Where-Object {$_.Name -ne "Administrator"} | Remove-LocalUser -WhatIf  
    

    If you execute one of the lines (line 2 or 4) it will show what will happen and which users will be disabled/deleted.
    If the result fits your requirement just delete the -WhatIf at the end of the line and execute the line again.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.