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 (22)

  1. Navish says:

    I used this and its works awesome. Thanks Amit.

  2. raghukarthika says:

    Try this script. download it and run.. if useful comment

    hotfile.com/…/Creare_Bulk_Users_in_AD.zip.html

  3. PS1Scripting says:

    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.html

  4. Hiren says:

    Thanks this is working great with a small modification. I added a semi comma after 2nd line

  5. Cody says:

    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!

    Cody

  6. Cody says:

    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!

    Cody

  7. Cody says:

    Hmm, any ideas?:

    "Unexpected token 'New-ADUser' in expression or statement.

    At line:1 char:125

    • Import-Csv .usercreationfile.csv | foreach-object { $userprinicpalname = $_.SamAccountName + "@domain.local" New

    -ADUser <<<<  -SamAccountName $.SamAccountName -UserPrincipalName $userprinicpalname -Name $.name -DisplayName $_.nam

    e -GivenName $.cn -SurName $.sn -Department $_.Department -Path "CN=Users,DC=biogen,DC=com" -AccountPassword (Convert

    To-SecureString "Microsoft~1;" -AsPlainText -force) -Enabled $True -PasswordNeverExpires $True -PassThru }

       + CategoryInfo          : ParserError: (New-ADUser:String) [], ParentContainsErrorRecordException

       + FullyQualifiedErrorId : UnexpectedToken"

  8. Cody says:

    Sorry to post so much but this isn't working out like it should.  I fixed the above error.  Using Hiren's suggestion I put a ; after the "@{domainname}.com".  However, now it's complaining about the account name.

    New-ADUser : The name provided is not a properly formed account name

    t line:1 char:126

    It errors out with that for every account in the CSV file.  Is this a formatting issue with the CSV file?  What kind of formatting should it be saved in?  There are several kinds of CSV formats available to save as (MS-DOS, comma-delimited, etc).

    Thanks,

    Cody

  9. @Cody:  I would suggest that you should be using a comma separated list of values, with the last entry per line indicated by CRLF.  This is implicit in the use of the term "csv", which means "Comma Separated Values".  Technically speaking, the use of TAB characters to separate the same value in a text file will result in a "tsv" file.

  10. John says:

    I get an error Get-Process : A Positional parameter cannot be found that accepts arguments 'sAMAccountName'

    this is my .ps1 code

    Import-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 }

  11. ARod says:

    @John: The file in question is a csv file as it was saved as so. I'm not sure what you meant by:

    "ith the last entry per line indicated by CRLF"

    Please elaborate as I'm also getting the same error as Cody. The script looks like it should work though. Thanks in advance.

  12. ARod says:

    Correction: My last question was aimed @Dave Robinson.

  13. ARod says:

    @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.

  14. Custaard says:

    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.

  15. Thorkell says:

    Hi Amit

    Regarding 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,

    Thorkell

  16. John says:

    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 $.Password

  17. John says:

    I guess there are lots of mistakes here.

  18. sean says:

    it works!There is some thing different from the above commands.

    import-Csv .users.csv | foreach-object{$userprinicpalname = $_.SamAccou

    ntName + "@test.com"; New-ADUser -SamAccountName $_.SamAccountName -UserPrincipa

    lName $userprinicpalname -Name $.name -DisplayName $.name -GivenName $_.cn -Su

    rName $.sn -Department $.Department -Path "OU=crm,DC=test,DC=com" -AccountPass

    word (ConvertTo-SecureString "Microsoft~1;" -AsPlainText -force) -Enabled $True

    -PasswordNeverExpires $True -PassThru }

  19. Juan Medina says:

    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.

  20. jkjk says:

    Thanks, that is exactly what I needed and it worked!!

  21. ADMaster says:

    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: http://www.adaxes.com/tutorials_ActiveDirectoryManagement_ImportUserAccountsFromCSVFile.htm

Skip to main content