Managing Local Accounts through PowerShell
When managing Local Accounts through PowerShell, it’s possible to use the [ADSI] type adapter.
Starting off, let’s look at how to connect to the Local Computer.
PS > $ComputerName = $env:COMPUTERNAME PS > $Computer = [adsi]"WinNT://$ComputerName" PS > $Computer distinguishedName : Path : WinNT://Computer01Now that we have a variable holding the reference to our local computer, we can go ahead and add a Local User Account. This is done through the Create Method on the object. we’ll also add a Password for our User.
PS > $UserName = "NewUser"
PS > $Password = "Password1"
PS > $User = $Computer.Create("user",$UserName)
PS > $User.SetPassword($Password)
PS > $User.SetInfo()
And it’s as simple as that. If you’r running Windows 7 you have to start PowerShell with elevated rights in order to get it to work.
Removing the User is done by using the Delete() method.
PS > $Computer.Delete("user",$UserName)
Here’s a script i wrote that adds, removes or resets the password of a local User Account.
Click here to download the script
Examples on running the Script:
PS > Set-LocalAccount.ps1 -UserName NewUser -Password Password1 -Add PS > Set-LocalAccount.ps1 -UserName NewUser -Password Password2 -ResetPassword PS > Set-LocalAccount.ps1 -UserName NewUser -Remove
[?]
