Creating Bulk Users in Active Directory Using PowerShell
We had a requirement of creating bulk users in Active directory for Test users. Initially i thought of wrtting .NET code which will communicate to LDAP and creates the users.
but i was not aware of that it can be done very-2 quickly using the Powershell with 3-4 line of scripts.
we had a csv file which had all the users with AD information in it. The Format of that csv is some thing like the below one:
Please change the {Domain Name} with your Environment Domain name.
Below is powershell script which will create users in Active directory, please ensure to run this powershell script in "Active Directory Module For Windows PowerShell".
UserCreationfile.csv is a csv filename which has all users information.
Import-Csv .\usercreationfile.csv | foreach-object {
$userprinicpalname = $_.SamAccountName + "@{domainname}.com"
New-ADUser -SamAccountName $_.SamAccountName -UserPrincipalName $userprinicpalname -Name $_.name -DisplayName $_.name -GivenName $_.cn -SurName $_.sn -Department $_.Department -Path "CN=Users,DC=biogen,DC=com" -AccountPassword (ConvertTo-SecureString "Microsoft~1;" -AsPlainText -force) -Enabled $True -PasswordNeverExpires $True -PassThru }
Once we Execute above powershell statements, we will see the results like the below:
Hope this will help.
Happy Coding !!!
Comments
Anonymous
February 06, 2012
I used this and its works awesome. Thanks Amit.Anonymous
February 27, 2012
Try this script. download it and run.. if useful commenthotfile.com/.../Creare_Bulk_Users_in_AD.zip.htmlAnonymous
July 19, 2012
I have a tutorial on my blog that breaks all this out step by step. Here it is: ps1scripting.blogspot.com/.../powershell-working-with-csv-files.htmlAnonymous
October 27, 2012
The comment has been removedAnonymous
September 16, 2013
Is there a way to add "first name" and "last name" as well? I'm trying to generate bulk users with First.Last for the username, Last, First for the display name, and also have it fill out the first and last name sections of the account.Thx!CodyAnonymous
September 16, 2013
Actually I think your script does this.. I had to read through the code and figured that the name is the display name, cn is the given name (First) and sn is the surname. I'll give this a try and post back if this script works out.Thanks for posting this info! It's really helpful!CodyAnonymous
September 16, 2013
The comment has been removedAnonymous
September 16, 2013
The comment has been removedAnonymous
October 10, 2013
The comment has been removedAnonymous
November 08, 2013
I get an error Get-Process : A Positional parameter cannot be found that accepts arguments 'sAMAccountName'this is my .ps1 codeImport-Csv .adc.csv | foreach-object {$userprinicpalname = $_.sAMAccountName + "@dod.test" PS > New-ADUser sAMAccountName $.sAMAccountName -UserPrincipalName $userprinicpalname -Name $.name -DisplayName $.name -GivenName $.cn -SurName $.sn -Department $.Department -Path "CN=Users,DC=dod,DC=test" -AccountPassword (ConvertTo-SecureString "Microsoft~1;" -AsPlainText -force) -Enabled $True -PasswordNeverExpires $True -PassThru }Anonymous
January 26, 2014
Go with this,adsysnet.com/asn-active-directory-manager-bulk-objects-creation.aspxReally nice tool for users creation,modification in bulk.get this from here: adsysnet.com/asn-active-directory-manager-download.aspxAnonymous
January 29, 2014
The comment has been removedAnonymous
January 29, 2014
Correction: My last question was aimed @Dave Robinson.Anonymous
January 29, 2014
@Cody: I found out the issue to your problem. I hope this helps as it fixed the same exact error for me. You need to make sure that your -Path is correct. I had this set up for it:-Path "CN=office users,DC=domain,DC=local" (This caused it to error out because of "office users")Once i changed it to:-Path "CN=users,DC=domain,DC=local" (I changed it to just "users" and it worked. I then moved it to the correct OU once it was in Active Directory)I guess it didn't like the space in between "office users". You have to love the way microsoft coded their product. Anyway, another reason to learn programming which will save you ton of headache :) . Also, make sure you put the semi-colon right before New-ADUser. I hope this helps.Anonymous
February 12, 2014
AROD, actually it would have worked with the space you just needed to change the CN to OU. At least that worked for me. As I was adding the users to an OU that I had created.Anonymous
February 13, 2014
Hi AmitRegarding password. when I ran the script all accounts have the same password "Microsoft~1;". What I expecting was to generate a different pwd for each accounts. How can I import the password section, that is giving in the excel column, so each accounts have a different password when they log on?Best regards,ThorkellAnonymous
April 04, 2014
Don't see the use of $.Password? That's why they are set to "Microsoft~1;". In his csv, they are all set to the same pass anyway. If you want different passwords then you have to fix the code to use $.PasswordAnonymous
April 30, 2014
I guess there are lots of mistakes here.Anonymous
October 15, 2014
it works!There is some thing different from the above commands.import-Csv .users.csv | foreach-object{$userprinicpalname = $.SamAccountName + "@test.com"; New-ADUser -SamAccountName $.SamAccountName -UserPrincipalName $userprinicpalname -Name $.name -DisplayName $.name -GivenName $_.cn -SurName $.sn -Department $.Department -Path "OU=crm,DC=test,DC=com" -AccountPassword (ConvertTo-SecureString "Microsoft~1;" -AsPlainText -force) -Enabled $True-PasswordNeverExpires $True -PassThru }Anonymous
September 04, 2015
You need to put the password before the Path to use it as parameter because the Path has the comma character and it breaks the retrieval of columns.Anonymous
October 30, 2015
Thanks, that is exactly what I needed and it worked!!Anonymous
November 02, 2015
It's good for test users, but if you are doing it on a regular basis and have a constant flow of CSVs from the HR dept, you would want to automate it. We're using this and it does help a lot: www.adaxes.com/tutorials_ActiveDirectoryManagement_ImportUserAccountsFromCSVFile.htm