May 052011
Recently I was creating a new demo environment and had to create some AD users to use in my CRM deployment. I hate doing tedious repetative tasks one-by-one, and ended up using a modified version of the powershell script from: http://www.thejoyofcode.com/Creating_AD_user_accounts_in_PowerShell.aspx – so I figured I’d share it with everyone else so that when they need to mass create some users and just have their first and last name, this will help.
Here is the powerhsell code:
$domain = (get-addomain).distinguishedname
$path = "OU=Employees,$domain"
$employees = Get-ADObject -Filter {distinguishedname -eq $path}
If ( -not $employees) {
$employees = $domain.Create("OrganizationalUnit", "ou=Employees")
$employees.SetInfo()
}
$users = import-csv "C:\Scripts\usersToBeCreated.csv"
$ldappath = "LDAP://$path"
$container = [ADSI] $ldappath
$users | foreach {
$first = $_.FirstName
$last = $_.LastName
$username = "$first" + "." + "$last"
$email = "$username" + "@demo.local"
$newUser = $container.Create("User", "cn=" + $username)
$newUser.Put("sAMAccountName", $username)
$newUser.Put("givenname",$first)
$newUser.Put("sn",$last)
$newUser.Put("mail",$email)
$newUser.Put("description","Demo Account")
$newUser.SetInfo()
$newUser.psbase.InvokeSet('AccountDisabled', $false)
$newUser.SetInfo()
$newUser.SetPassword("somethingG00D!")
}
Hopefully that will help someone else save some time.
10 Responses to “Quickly Create Users in Active Directory from CSV for Demo Environment”
Comments (7) Pingbacks (3)

Hi, Carlton, we met a new problem now, and I guess it may cause of migration from CRM4 to CRM2011,
in QueueItem detail form, if the date is upgrated from CRM4, “Worked By” field has data, but lack user’s display name.
Check this thread, i did post some captures within it.
Neil, Have you checked out this technet article? It may help.
On a side note, if you updated from CRM 4 to CRM 2011 and the full name was not set, it may be an issue with the data in the db. Until you figure it out completely you could add the Full Name from the User entity to the queue item view.
And there is sth else i want to know, why there is no “Retrieve/RetrieveMultiple” Message On “QueueItem”. Thanks for you patience and answers.
Have you opened a support ticket for this? Do you have a block of code or something that I can use to test this? According to the documentation at: http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.messages.retrieverequest.aspx and http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.messages.retrievemultiplerequest.aspx queueitem should support both Retrieve and RetrieveMultiple messages. What was your use case for this?
Sorry, I should have described it more detailed, I was trying to create a plugin to change Title of QueueItems. But according to “CRMSK\tools\message-entity support for plug-ins.xlsx”, “Retrieve” and “RetrieveMultiple” are not supported. My requirement is that, some data( such as Case Title) need be encrypted when create, and be decrypted before display on page, we use plugin to achieve it. If any case is added to a Queue, we need decrypt is as well, but for QueueItem, we didnt find out any Message when Retrieve/RetrieveMultiple.
That document doesn’t specify it, but the other two links I provided do specify that queueitem is supported for retrieve and retrievemultiple, which is why I would suggest contacting support. It could be a bug or it could be a problem with the documentation.