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: Powershell, Active Directory
IceRocket tags: Powershell, Active Directory
del.icio.us tags: Powershell, Active Directory
UPDATED
lease 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()
}
thats a great contribution!
By: markoh on September 14, 2007
at 11:55 am
Someone got it work?
The following exception occurred while retrieving member “Create”: “There is no such object on the server.
By: Dmitriy Ilyin on March 10, 2010
at 9:45 am
Your code appears to be cutoff but the right-hand sidebar. I’d really like to see all of it.
By: Ben on June 16, 2010
at 4:23 pm
[...] http://techstarts.wordpress.com/2007/02/21/bulk-user-creation-using-powershell-2/ [...]
By: PowerShell Scripting | The Digital Home of Brent Lee on October 7, 2011
at 2:13 am