Posted by: Preetam | February 21, 2007

Bulk User Creation using PowerShell

When we think of making any changes in AD, we must start at creating objects. And as always first thing that comes to Mind is to create multiple users. And again here reference are same as previous one’s MOW and Arulk. MOW’s concept is used but methods I have to use from Arulk, cause things work.

Usually you get a excel sheet from the HR etc dept, I would always convert it into CSV since PowerShell will do the magic for me.

Contents of CSV file [Busers.csv]

Below are the headers of the CSV file and you can fill in the data.

CN,SN,GivenName,Name,Title,Description,PostalCode,TelephoneNumber,Department,Company,StreetAddress,Countrycode,SamAccountName,userPrincipalName,Mail,HomePhone,mobile.

I’ve shared the CSV file using google docs. Click Here

I’m going to create bulk users in India OU, which is under Zarays’ OU

$IndiaOU=[ADSI]LDAP://localhost:389/ou=India,dc=zarays,dc=com
# Connecting to India OU

$UserDetails=Import-Csv “Buser.csv” #—–importing bulkusers data

foreach($UD in $UserDetails) { #——–looping into csv file and

# Passing all data into variables
$CN=$UD.CN
$SN=$UD.SN
$title=$UD.title
$description=$UD.description
$department=$UD.department
$streetAddress=$UD.streetAddress
$postalcode=$UD.postalcode
$telephoneNumber=$UD.telephoneNumber
$givenName=$UD.givenName
$company=$UD.company
$mail=$UD.mail
$homePhone=$UD.homePhone
$mobile=$UD.mobile
$userPrincipalName=$UD.userPrincipalName
$Samaccountname=$UD.Samaccountname

$Indiauser=$IndiaOU.create(“user”,”cn=$cn”) #I created actual user here and later I’m

#filling all properties for user
$Indiauser.Put(“sAMAccountName”,$Samaccountname)
$Indiauser.put(“SN”,$SN)
$Indiauser.put(“Title”,$Title)
$Indiauser.put(“Description”,$description)
$Indiauser.put(“department”,$department)
$Indiauser.put(“streetAddress”,$streetAddress)
$Indiauser.put(‘Postalcode’,$postalcode)
$Indiauser.put(‘telephoneNumber’,$telephoneNumber)
$Indiauser.put(‘givenName’,$givenName)
$Indiauser.put(‘company’,$company)
$Indiauser.put(‘mail’,$mail)
$Indiauser.put(‘homePhone’,$homePhone)
$Indiauser.put(‘mobile’,$mobile)
$Indiauser.put(‘userPrincipalName’,$userPrincipalName)

$Indiauser.setinfo() #All data committed. There are lots of other properties you can

#add
}

Below here I’m doing two things, First is enable the user and second set password. Because for some reason I’m not able to get these two things done in above loop. It throws exception. But I’m sure it can be included.

$IndiaOU=new-object directoryservices.directoryentry(“LDAP://ou=India,dc=zarays,dc=com”)
$UserDetails=Import-Csv “Buser.csv”

$userdetails=$IndiaOU.psbase.Children

foreach($UD in $UserDetails) {
$CN=$UD.CN
$accts=$IndiaOU.psbase.Children.Find(“cn=$CN”)
$accts.psbase.Invoke(“SetPassword”,”P@ssW0Rd”)
$accts.psbase.InvokeSet(‘Accountdisabled’,$false)
$accts.psbase.CommitChanges()
}

Last and least, I’m sure there much better CMDlets to do this job, but what makes me proud it that I’ve created this script and I understand it very well. As compare to those VBScript scripts available on the net, I can use it but can’t customize it. Thanks GOD there is PowerShell Team of Blogs.

Technorati tags: ,

IceRocket tags: ,

del.icio.us tags: ,

UPDATED :Please check comment where you will see the reason Why code has been updated.

Marc !! Bingo here it goes..the way it should work.

$IndiaOU=[ADSI]“LDAP://localhost:389/ou=Singapore,dc=zarays,dc=com“$UserDetails=Import-Csv “latestusers.csv”
foreach($UD in $UserDetails) {

$CN=$UD.CN

$SN=$UD.SN

$title=$UD.title

$description=$UD.description

$department=$UD.department

$streetAddress=$UD.streetAddress

$postalcode=$UD.postalcode

$telephoneNumber=$UD.telephoneNumber

$givenName=$UD.givenName

$company=$UD.company

$mail=$UD.mail

$homePhone=$UD.homePhone

$mobile=$UD.mobile

$userPrincipalName=$UD.userPrincipalName

$Samaccountname=$UD.Samaccountname

$Indiauser=$IndiaOU.create(“user”,”cn=$cn”)

$Indiauser.Put(“sAMAccountName”,$Samaccountname)

$Indiauser.put(“SN”,$SN)

$Indiauser.put(“Title”,$title)

$Indiauser.put(“Description”,$description)

$Indiauser.put(“department”,$department)

$Indiauser.put(“streetAddress”,$streetAddress)

$Indiauser.put(‘telephoneNumber’,$telephoneNumber)

$Indiauser.put(‘givenName’,$givenName)

$Indiauser.put(‘company’,$company)

$Indiauser.put(‘mail’,$mail)

$Indiauser.put(‘homePhone’,$homePhone)

$Indiauser.put(‘mobile’,$mobile)

$Indiauser.put(‘userPrincipalName’,$userPrincipalName)

$Indiauser.setinfo()

$Indiauser.psbase.Invoke(“SetPassword”,”P@ssW0Rd”)

$Indiauser.psbase.InvokeSet(‘Accountdisabled’,$false)

$Indiauser.psbase.CommitChanges()

}

About these ads

Responses

  1. thats a great contribution!

  2. Someone got it work?

    The following exception occurred while retrieving member “Create”: “There is no such object on the server.

  3. Your code appears to be cutoff but the right-hand sidebar. I’d really like to see all of it.

  4. [...] http://techstarts.wordpress.com/2007/02/21/bulk-user-creation-using-powershell-2/ [...]

  5. Exception calling “create” with “2″ argument(s): “An invalid directory pathname was passed

    the create statement does not work


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

Categories

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: