Monday, December 23, 2019

PowerShell: Removing all active Local Users

This code will allow you to remove active Local Windows Users who are not service or built-in Local Administrator:

$LocalUsers = Get-LocalUser
Foreach ($LocalUser in $LocalUsers) {
If (!($LocalUser.SID.Value -match ".*-5\d\d") -And $LocalUser.Enabled) {
Write-Host "Removing user:"$LocalUser.Name
Remove-LocalUser $LocalUser }}


You must run the script with elevated privileges.

No comments:

Post a Comment