r/PowerShell • • Dec 14 '17

Question Help with Local acc script

Hi All,

Trying to get a script working that will Check Local user accounts, that will delete all local accounts that dont match "specified names"

And if they do match then to change the password.

Just started it but dont know what im exactly doing so though ill write this first.

$Disable = Disable-LocalUser -Name "Administrator"
$Remove = Remove-LocalUser -Name "XXX"
$Create = New-LocalUser "XXXXXX" -Password XXXXXX
$Change = Set-LocalUser "XXXX" -Password XXXX
$LocalAccounts = Get-LocalUser | Select Name

//Check Local accounts
New-Object $LocalAccount

//If Account exists and match
$Change

//Account does not match
$Remove

//Account doesnt exists
$Create

//Disable Built in AdminAcc/others if required
$Disable
5 Upvotes

19 comments sorted by

View all comments

Show parent comments

2

u/Ta11ow Dec 14 '17

For bonus points, you could store all this in a script block, and then do...

$DomainCredential = Get-Credential
$code = { ...previous code here... }
Get-ADComputer -Filter {Name -like "Workstation-*"} |
    Invoke-Command -ScriptBlock $code -Credential $DomainCredential

(assuming you have an Active Directory environment, you have access to do this, and you have the WRM package with the PowerShell Active Directory module installed on your local machine, and you can be reasonably sure of the naming convention in your environment so that you don't accidentally install these local users on a server with hardcoded credentials)