Home > Active Directory, PowerShell > Create new users with unique Passwords in Active Directory using CSV File and PowerShell

Create new users with unique Passwords in Active Directory using CSV File and PowerShell


Here is simple PowerShell script that can create new accounts and setup unique passwords in Active Directory.

First Step is to create a csv file and specify rows for new accounts For e.g.

UserName,FirstName,LastName,Password,PrincipalName

Second Step is to the script below using PowerShell

$objOU=[ADSI]”LDAP://OU=<OUName>,DC=<test>,DC=<com>”


$dataSource=import-csv “test.csv” //This is the name of the csv file

foreach($dataRecord in $datasource) {


$cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName
$sAMAccountName=$dataRecord.UserName
$givenName=$dataRecord.FirstName
$sn=$dataRecord.LastName
$sAMAccountName=$sAMAccountName.ToLower()
$displayName=$sn + “, ” + $givenName
$Password = $dataRecord.Password
$userPrincipalName=$sAMAccountName + “@test.com”
$emailAddress=$dataRecord.PrincipalName
$objUser=$objOU.Create(“user”,”CN=”+$cn)
$objUser.Put(“sAMAccountName”,$sAMAccountName)
$objUser.Put(“userPrincipalName”,$userPrincipalName)
$objUser.Put(“displayName”,$displayName)
$objUser.Put(“givenName”,$givenName)
$objUser.Put(“mail”,$emailAddress)
$objUser.Put(“sn”,$sn)
$objUser.SetInfo()
$objUser.SetPassword($Password)
$objUser.psbase.InvokeSet(“AccountDisabled”,$false)
$objUser.SetInfo()


}

Last Step is to verify in Active Directory that the accounts are created.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: