Creating Local Admins

by Sep 12, 2011

Here is a piece of code that will create a local user account and put it into the local Administrators group:

$computername = $env:computername   # place computername here for remote access
$username = 'AdminAccount1'
$password = 'topSecret@99'
$desc = 'Automatically created local admin account'


$computer = [ADSI]"WinNT://$computername,computer"
$user = $computer.Create("user", $username)
$user.SetPassword($password)
$user.Setinfo()
$user.description = $desc
$user.setinfo()
$user.UserFlags = 65536
$user.SetInfo()
$group = [ADSI]("WinNT://$computername/administrators,group")
$group.add("WinNT://$username,user")

You do need local Admin privileges to execute this script. Adjust the name of the "Administrators" group to match your locale. For example, on German systems, it is called "Administratoren".

Twitter This Tip!
ReTweet this Tip!