Advertisement
mikedopp

RemoveAdminAddNewAdminUser.ps1

Nov 6th, 2020
2,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $propADComputers = Get-ADComputer -Filter * -SearchBase "Whatever you want"
  2. $name = Read-Host -AsSecureString "Enter the name of the non-built in admin you would like if required:"
  3. $password = Read-Host -AsSecureString "Enter the password of the non-built in admin account you would like if required:"
  4. foreach ($c in $propADComputers | Select-Object -ExpandProperty "Name")
  5. {
  6.        
  7.     try{
  8.        
  9.         #Start remote PSSession to PC
  10.         Write-Host "Attempting to enter PSSession for $c..."
  11.         Enter-PSSession $c
  12.         Write-Host "Success!"
  13.        
  14.         #Local Built-In Admin Account & account information
  15.         Write-Host "Fetching built-in admin account..."
  16.         $builtinAdmin = Invoke-Command -ComputerName $c -ScriptBlock {Get-LocalUser | ? {$_.SID -like "*-500"}}
  17.         $name = ($builtinAdmin | Select-Object -ExpandProperty "Name").ToLower()
  18.         $isEnabled = $builtinAdmin | Select-Object -ExpandProperty "Enabled"
  19.        
  20.         if($name -contains "$name"){
  21.             "Built-in account name is $name...let's change it"
  22.             Invoke-Command -ComputerName $c -ScriptBlock {(Rename-LocalUser -Name $using:name -NewName "Administrator")
  23.         }
  24.         if ($isEnabled -eq $True){
  25.             "Built-in account is enabled...let's disable it."
  26.             #Need to run it again to get the new name
  27.             $builtinAdmin = Invoke-Command -ComputerName $c -ScriptBlock {Get-LocalUser | ? {$_.SID -like "*-500"}}
  28.             Invoke-Command -ComputerName $c -ScriptBlock {Disable-LocalUser -Name $Using:builtinAdmin | Select-Object -ExpandProperty "Name"}
  29.         }
  30.         Write-Host "Creating non built-in admin account..."
  31.         Invoke-Command -ComputerName $c -ScriptBlock {
  32.             (New-LocalUser -Name $Using:name -Password $Using:password -PasswordNeverExpires), (Add-LocalGroupMember -Group "Administrators" -Member $Using:name)
  33.         }
  34.     }      
  35.         Exit-PSSession
  36.     }
  37.     catch{
  38.         Write-Host "Unable to enter PSSession to $c"
  39.     }
  40. }
  41.        
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement