skip to Main Content

Create Active Directory Users from CSV with PowerShell

We want to create new Active Directory users in the organization. We can use the user creation wizard in Active Directory Users and Computers to create the users. What if you need to create 1000+ users? Doing it by the wizard will take a lot of time. In this article, you will learn how to create Active Directory Users from CSV with PowerShell.

Before you start to create Active Directory Users from CSV file

Let’s say you need one minute to create a new user with the wizard. It will take one minute for every user. What if you have to add a thousand new users? Do the math, 1000 users * 1 minute = 1000 minutes. That is approximately 16 hours. A bit too much time to spend on creating new users, right? What if you can simplify the process and do it much faster in time.

In the article, we are going to use the following files:

FileInfo
NewUsersSent.csvThe spreadsheet template that we sent to HR
NewUsersReceived.csvThe spreadsheet that we received from HR
NewUsersFinal.csvThe updated final version to import
Add-NewUsers.ps1The PowerShell script that will create the user accounts

Create the CSV template

First, we need to make sure that we have a list of all the thousand users we need to create. The most important information that we need is the first name and last name. We suggest to create a CSV file and send it to a user in the company.

We are going to send the CSV file to Jane. Jane works in the HR department at the company called EXOIP. She does have the information regarding the thousand users that need a new AD user account. The CSV file that we send to Jane is NewUsersSent.csv. Jane will fill in the data, just like discussed, and send the CSV file back to us.

Create Active Directory Users from CSV with PowerShell Excel NewUsersSent

We told Jane to put as much information she can in the fields. If she can’t fill in all the fields, not to worry. The most important is the First name and Last name. We also told her to leave the Password and OU field empty. Why did we tell her that?

  • Password field, leaving it empty because the IT department will create the passwords. Even if it’s a temporary password. When the user logs in, a prompt will show up to change the password.
  • OU field, leaving it empty because the IT department will fill in the Organizational Unit (OU). That’s where the new Active Directory (AD) Users are going to be created.

Edit the CSV file

Jane sent the CSV file back with all the information. Let’s open the NewUsersReceived.csv file and have a look.

Create Active Directory Users from CSV with PowerShell Excel NewUsersReceived

All is looking great. Jane did fill all the information that we need.

Note: Find your country code in the Active Directory country code list.

Generate passwords

Generate passwords for the users and fill the password fields for every user. I recommend generating passwords with Manytools password generator. You can create a maximum of 9999 passwords at one time. After that, copy and paste the passwords in the column Password.

Fill in the Organization Unit (OU)

Start Active Directory Users and Computers (ADUC) and make sure to enable Advanced Features. Click the menu View and click Advanced Features. Now that Advanced Features is enabled, we can proceed further and create the OU in AD.

We can see in the CSV file that the users are going to work in the IT department. We will create an OU in Active Directory with the name IT.

Right-click the OU with the name IT and click Properties. Click the tab Attribute Editor. Find the attribute distinguishedName. Double-click on it and copy the value. The value in my example is OU=IT,OU=Users,OU=Company,DC=exoip,DC=local.

Create Active Directory Users from CSV with PowerShell get distinguishedName

Place the value in the fields under the column OU.

Create Active Directory Users from CSV with PowerShell Excel fill OU

When done, save it as a new CSV file. Go to File and click Save As. Give it the file name NewUsersFinal.csv. Save it as type CSV UTF-8 (Comma delimited) (*.csv). Click the button Save.

Create Active Directory Users from CSV with PowerShell Excel save as Final CSV UTF-8

The NewUsersFinal.csv is created.

Create Active Directory Users from CSV with PowerShell Excel NewUsersFinal

Place the NewUsersFinal.csv in the C:\Temp folder on the Domain Controller or the Management Server.

Check the CSV file

Before using the script, import the CSV file in PowerShell. It’s an excellent way to check if it’s readable and if you’re all set.

Good to know about delimiter

Is the delimiter showing as a comma or semicolon in the CSV file? You need to know that if you are going to use the PowerShell script in the next step. If you use the semicolon as a separating character in your CSV file, add the delimiter parameter -Delimiter “;” to your Import-Csv cmdlet.

Run PowerShell as administrator. Use the Import-Csv cmdlet to read the CSV file. If it can’t read the CSV file, remove the -Delimiter parameter. It will be Import-Csv C:\Temp\NewUsersFinal.csv | Format-Table.

PS C:\> Import-Csv C:\Temp\NewUsersFinal.csv -Delimiter ";" | Format-Table

FirstName Initials Lastname Username        Email                      StreetAddress City   ZipCode State Country
--------- -------- -------- --------        -----                      ------------- ----   ------- ----- -------
Max       MF       Fraser   Max.Fraser      Max.Fraser@exoip.com       21 Baker St   London NW1 6XE       GB
Piers     PB       Bower    Piers.Bower     Piers.Bower@exoip.com      21 Baker St   London NW1 6XE       GB
Kylie     KD       Davidson Kylie.Davidson  Kylie.Davidson@exoip.com   21 Baker St   London NW1 6XE       GB
Richard   RG       Grant    Richard.Grant   Richard.Grant@exoip.com    21 Baker St   London NW1 6XE       GB
Boris     BC       Campbell Boris.Campbell  Boris.Campbell@exoip.com   21 Baker St   London NW1 6XE       GB
Nicholas  NM       Murray   Nicholas.Murray Nicholas.Murray@exoip.com  21 Baker St   London NW1 6XE       GB
Leonard   LC       Clark    Leonard.Clark   Leonard.Clark@exoip.com    21 Baker St   London NW1 6XE       GB
Ruth      RD       Dickens  Ruth.Dickens    Ruth.Dickens@exoip.com     21 Baker St   London NW1 6XE       GB
Jonathan  JF       Fisher   Jonathan.Fisher Johnathan.Fisher@exoip.com 21 Baker St   London NW1 6XE       GB
Grace     GR       Rees     Grace.Rees      Grace.Rees@exoip.com       21 Baker St   London NW1 6XE       GB

For more information, read Import CSV delimiter PowerShell.

Prepare the Add-NewUsers PowerShell script

Download the Add-NewUsers.ps1 script and save it in path C:\Scripts on the Management Server or Domain Controller.

  • Line 5 is the path of the CSV file. Change the path if you place the CSV file in another path. In our example, it’s C:\Temp\NewUsersFinal.csv.
  • Line 5, remove the -Delimiter parameter if you have a coma separating character instead of semicolon in your CSV file. In our example, it’s the semicolon character.
  • Line 8 is the UserPrincipalName (UPN). Change the UPN to yours. In our example, it’s exoip.com.

Run the Add-NewUsers PowerShell script

Change the directory path to C:\Scripts\ and run the script Add-NewUsers.ps1. The script will run and create Active Directory users in bulk. When done, press Enter to exit the screen.

PS C:\>cd C:\Scripts\
PS C:\Scripts> .\Add-NewUsers.ps1
The user account Max.Fraser is created.
The user account Piers.Bower is created.
The user account Kylie.Davidson is created.
The user account Richard.Grant is created.
The user account Boris.Campbell is created.
The user account Nicholas.Murray is created.
The user account Leonard.Clark is created.
The user account Ruth.Dickens is created.
The user account Jonathan.Fisher is created.
The user account Grace.Rees is created.
Press Enter to exit:

If the user already exists in AD, you will see the following.

PS C:\Scripts> .\Add-NewUsers.ps1
WARNING: A user account with username Max.Fraser already exists in Active Directory.
WARNING: A user account with username Piers.Bower already exists in Active Directory.
WARNING: A user account with username Kylie.Davidson already exists in Active Directory.
WARNING: A user account with username Richard.Grant already exists in Active Directory.
WARNING: A user account with username Boris.Campbell already exists in Active Directory.
WARNING: A user account with username Nicholas.Murray already exists in Active Directory.
WARNING: A user account with username Leonard.Clark already exists in Active Directory.
WARNING: A user account with username Ruth.Dickens already exists in Active Directory.
WARNING: A user account with username Jonathan.Fisher already exists in Active Directory.
WARNING: A user account with username Grace.Rees already exists in Active Directory.
Press Enter to exit:

Have a look in ADUC. The users are created successfully in the OU.

Create Active Directory Users from CSV with PowerShell check created users in AD

Did this help you to create Active Directory Users from CSV with PowerShell?

Keep reading: Bulk move AD users to another OU with PowerShell »

Conclusion

In this article, you learned how to create Active Directory Users from CSV with PowerShell. At first, prepare the CSV file and make sure all the information is filled in. When you have the final CSV file, import the CSV file in PowerShell to check if it’s readable. Adjust the two lines in the Add-NewUsers.ps1 script, as shown in the article. Run the script to create the users in AD. The last part is to check if the users are created successfully in ADUC.

Did you enjoy this article? If so, you may like Install Exchange Server prerequisites. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 43 Comments

  1. Hi,

    Like many here, I had the problem with the filter expression not evaluating properly.
    The following might be the solution you are looking for.

    So change:
    this if (Get-ADUser -F { SamAccountName -eq $username }) { …
    to this if (Get-ADUser -F “SamAccountName -eq `”$username`”” ) { …

    Hope This helps!

  2. Hi
    I get this error when trying, i have just one user in the .csv file.
    Any idea whats wrong?

    PS D:\AD> .\Add-NewUsers.ps1
    New-ADUser : A value for the attribute was not in the acceptable range of values
    At D:\AD\Add-NewUsers.ps1:41 char:9
    + New-ADUser `
    + ~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (CN=Henrik Franz…,DC=uc,DC=intra:String) [New-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8322,Microsoft.ActiveDirectory.Management.Commands.NewADUser

    Regard
    Mike

  3. How can one go about also adding the ability to map server $Homedirectory to this script for each user found on the CSV?

  4. Thank you very much works wonders for me! I do have a question.
    Let’s say the street address has 2 lines. If the address is the following:
    Random St.
    Suite 101
    How would you go about doing that? I tried to make another line on the csv, but it still shows up as 1 line on the address in AD.
    Thanks in advance!

    1. Hi Junel,
      I am getting this error while running.
      Could you please post your updated code? Not sure what went wrong with mine.

      New-ADUser : A value for the attribute was not in the acceptable range of values
      At C:\temp\scripts\Add-NewUsers.ps1:41 char:9
      + New-ADUser `
      + ~~~~~~~~~~~~
      + CategoryInfo : NotSpecified: (CN=Dev Deven,OU…C=adecco,DC=net:String) [New-ADUser], ADException
      + FullyQualifiedErrorId : ActiveDirectoryServer:8322,Microsoft.ActiveDirectory.Management.Commands.NewADUser

  5. HI Ali
    I get this Error :
    “Get-ADUser : Variable: ‘username’ found in expression: $username is not defined.
    At C:\ADUsers.ps1:25 char:9
    + if (Get-ADUser -F { SamAccountName -eq $username }) {
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    why is the username not defined ?

    1. 1. check .csv format of u file;
      2. In Line 5, remove -Delimiter “;” | Format-Table parameter

  6. Hello,

    Sorry for the late message…

    Thank a lot for the scripts.

    Anyway, it used to work but suddenly iget the following error (my system is in French):

    Get-ADUser : Le filtre de recherche n’est pas reconnu
    Au caractère D:\AD\Add-NewUsers.ps1:32 : 9
    + if (Get-ADUser -F { SamAccountName -eq $username }) {
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    Here are the line form my script:

    # Check to see if the user already exists in AD
    if (Get-ADUser -F { SamAccountName -eq $username }) {

    # If user does exist, give a warning
    Write-Warning “A user account with username $username already exists in Active Directory.”
    }

  7. hi I want to add/import bulk users with multiple fields in local AD.

    First Name
    Last Name
    Display Name
    Mobile Number
    E-mail
    Job Title
    Department
    Company
    OU
    , can you suggest a PowerShell command for the same. ?

  8. Does not work on a fully patched W2k19 DC

    Get-ADUser : The search filter cannot be recognized
    At C:\_temp\Add-NewUsers.ps1:34 char:10
    + if ((Get-ADUser -LDAPFilter “(SamAccountName=$username)”)) {
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    1. The script works on all Windows Server versions. What I can see from the error is that you removed a couple of lines from the script. Have a closer look after your adjustments, and you hopefully figure it out.

  9. Hi Ali
    Thank you for the absolut genius powershell, it work very well.. One question, is it possible to add AD Groups in the same script? So i can make several different powershell scripts for different OUs whit differt accsess?
    br
    Ivo

  10. Thanks for the info. Great Script. I want to add multiple proxy addresses for each user. I not very good at ps (just learning). I am unable to figure out how to do this. Any help would be appreciated. I have been researching for hours and testing with no luck.

  11. hello,

    i am testing the script and am getting the following erors:

    Get-ADUser : Variable: ‘username’ found in expression: $username is not defined.
    At C:\scripts\script2.ps1:31 char:9
    + if (Get-ADUser -F { SamAccountName -eq $username }) {
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    any idea what is going wrong?
    thank you

    1. Ensure on line 5: Import-Csv C:\Temp\NewUsersFinal.csv -Delimiter “;” Ensure on Line 5, you change the “;” to “,” your problem will be resolved

  12. Hi! Nice job, your script really helped me. Is there a way to define flags like “Password never expires” with a similar script?

    Thanks in advance.

  13. Is there a way to add these new users to groups in the same script? Or does there need to be a secondary script just for that?

    1. I added this line in to my version of the script (in the section of rows 44-65)
      -OtherAttributes @{pager=$User.pager} `

  14. Thank you so much for this Ali Tajran! I’ve been trying to make a script working the whole day and yours worked perfectly! Thanks man! Really appreciate it!

    -Stian

  15. Muchas gracias por la información Ali, super bueno el aporte, sin embargo me sale un error al ejecutar el script:

    PS C:\Scripts> C:\Scripts\CreateUsersSSO.ps1
    En C:\Scripts\CreateUsersSSO.ps1: 11 Carácter: 10
    + foreach ($ User en $ ADUsers) {
    + ~
    Falta el nombre de variable después de foreach.
    En C:\Scripts\CreateUsersSSO.ps1: 11 Carácter: 29
    + foreach ($ User en $ ADUsers) {
    + ~
    Token ‘)’ inesperado en la expresión o la instrucción.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingVariableNameAfterForeach

  16. Hi, i get following error messages after the Accounts have been successfully created:
    Get-ADUser : The search filter cannot be recognized
    At C:\Users\x.ctue\Desktop\Create_ADUser_Bulk.ps1:29 char:9
    + if (Get-ADUser -F { SamAccountName -eq $username }) {
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    so you have any sugestions?

    Thanks in advance.
    Sincerely
    Chris Turk

    1. Import-Csv C:\Temp\NewUsersFinal.csv -Delimiter “;” Ensure on Line 5, you change the ; to , your problem will be resolved

  17. thank you Ali for the amazing material.

    it works great however the Country will not be inserted in the AD User Properties.
    I’m using AD 2012 R2

    I have tried to add countrycode (380 in my case) for Italy in your script but I get errors

    1. You’re welcome, and I am glad you followed the steps to create AD users with PowerShell. That will save you some time.

      After having a look, I adjusted the CSV file and the PS script. I sent it in a .zip file to you by email. Let me know if that worked!

      I will update the article this week with these changes.

      One more thing, use “IT” as the country code for Italy.

  18. Hi
    Have you any idea what this is then I run the script?
    Thanks //marsk

    Read-Host -Prompt “Press Enter to exit”
    cmdlet New-ADUser at command pipeline position 1
    Supply values for the following parameters:
    Name:

Leave a Reply

Your email address will not be published. Required fields are marked *