Tech

Powershell script to delete local machine account

I am writing this below script using this I need to delete all the user accounts on the local machine which are not built-in accounts or the IT support AD account called “Domain.Local”. However, my every attempt to do this always ends the same way, I can only get my power-shell script to list out all the local user accounts and not any of the active directory accounts logged into the machine. Some assistance would be appreciative.

Below is my script:

$localUsers = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | Select-Object -ExpandProperty Name
foreach ($localUser in $localUsers){
  if (($localUser -eq "ITSMELSS") -or ($localUser -eq "Administrator") -or ($localUser -eq "defaultuser0") -or ($localUser -eq "Default Profile") -or ($localUser -eq "DefaultAccount") -or ($localUser -eq "Guest") -or ($localUser -eq "WDAGUtilityAccount")) {
  Write-Host "Keeping:"$localUser
  }else {
  Write-Host "Deleting:"$localUser
  #Remove-LocalUser -userName $localUser
  }
  }
1 Like

Hi @TimCook,

Trying something like this:

This script uses a label to allow the “continue” statement to jump to the next iteration of the outer loop, instead of the inner loop. You’ll find details on this technique in the about_Break help file, though the Continue statement supports them as well.

$Users = Get-WmiObject -Class Win32_UserProfile
$IgnoreList = "DAdmin", "LAdmin", "Default", "NetworkService", "LocalService", "SystemProfile"

:OuterLoop
foreach ($User in $Users) {
    foreach ($name in $IgnoreList) {
        if ($User.localpath -like "*\$name") {
            continue OuterLoop
        }
    }

    $User.Delete()
}

or like this:

$Users = Get-WmiObject -Class Win32_UserProfile
$IgnoreList = "DAdmin", "LAdmin", "Default", "NetworkService", "LocalService", "SystemProfile"

$pattern = ($IgnoreList | ForEach-Object { [regex]::Escape($_) }) -join '|'
$pattern = "\\(?:$pattern)$"

foreach ($User in $Users) {
    if ($User.LocalPath -notmatch $pattern)
    {
        $User.Delete()
    }
}
1 Like

Deletes local user accounts.

Syntax

PowerShellCopy

      [-WhatIf]
      [-Confirm]
      []" style="box-sizing: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; direction: ltr; position: relative; border: 0px; padding: 0px; display: block; line-height: 19px;">Remove-LocalUser
      [-InputObject] <LocalUser[]>
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]

PowerShellCopy

      [-WhatIf]
      [-Confirm]
      []" style="box-sizing: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; direction: ltr; position: relative; border: 0px; padding: 0px; display: block; line-height: 19px;">Remove-LocalUser
      [-Name] <String[]>
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]

PowerShellCopy

      [-WhatIf]
      [-Confirm]
      []" style="box-sizing: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 1em; direction: ltr; position: relative; border: 0px; padding: 0px; display: block; line-height: 19px;">Remove-LocalUser
      [-SID] <SecurityIdentifier[]>
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]

Description

The Remove-LocalUser cmdlet deletes local user accounts.