Thursday, 12 March 2015

Power Shell Script to create Local User Account & Set Password and add into Local Administrator Group


The below script will perform the following :


  1.  Create Local user on each Server
  2.  Set Password for newly created account
  3.  Add newly created Admin account to Local Administrator group 

Csv file format is as below:



#-----------------------------------------------------------------------------------------------------#
#Script Title : Create local user on each server and add local user account onto Administrator group
#changes in Script : change csv file path in first line of script
#------------------------------------------------------------------------------------------------------#

$list =  Import-CSV "C:\LocalAdmins\testUsers.csv" #change path here..
foreach($Servers in $list) {

try{

$computer = $Servers.ServerName
$user = $Servers.LocalAdminAccount
$pass = $Servers.Password

$objOu = [ADSI]"WinNT://$computer"
$objUser = $objOU.Create("User", $user)
$objUser.setpassword($pass)
$objUser.SetInfo()
$objUser.description = "Local Admin Account"
$objUser.SetInfo()

Write-Host " Admin Account created Successfully  for $computer with password as $pass" -ForegroundColor "Green" -BackgroundColor "Black"

$AdminGroup = [ADSI]"WinNT://$computer/Administrators,group"
$AdminGroup.Add("WinNT://$computer/$computer/$user,user")

Write-Host "Account $user added successfully into local adminstrator group on Server $computer"  -ForegroundColor "Yellow" -BackgroundColor "Black"
}

Catch
{
Write-Host " Unable to reach $computer Server" -ForegroundColor "yellow" -BackgroundColor "red"
}

}

2 comments:

  1. Hi,
    if i want to execute this on a local server, how do i have to change the script?

    Tamer

    ReplyDelete
    Replies
    1. For example, i have an exisiting Administrator User with name Administrator, how can i force the script to change the existing password?

      Delete