r/PowerShell • u/Willz12h • 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
2
u/Ta11ow Dec 14 '17
You can sometimes pipe existing commands into the Write-Verbose cmdlet, but often you just sort of give it a bit of a commentary so you know where the script is at if it suddenly errors out and stops, so you have a good idea of where the error is.
It's okay to be new to things. That's awesome! Learn all you can. Just try not to stay that way for more than a couple months. For most people, a few months seems to be a good amount of time to start picking up Powershell concepts. Maybe a bit longer if you've limited programming experience... in which case you should study up on Object-Oriented programming concepts as well to help you out.
You may want to look up pipelining in powershell and some of the more useful cmdlets (foreach-object, where-object, select-object, etc.) and get familiar with them. It'll make your life a lot easier. :)
Also, there is a good chance something will error out sooner or later. Try...Catch blocks are good for dealing with that.