Importing Users into Active Directory from a CSV File Using PowerShell

There are two aspects to this post – first is the CSV file with the user data and then there’s the PowerShell script to import the data into Active Directory. As an example, this how-to post will only set the first name, last name, username and password values for our user objects.

Preparing the CSV file

A template of the CSV file with sample data can be downloaded here (save it with a .csv extension). Here’s what it looks like:

CSV template

Populate the CSV file with the user data, making sure you enter the Distinguished Name of the Organisational Unit in the OU field.

Follow these instructions to check the format of an OU’s distinguished name:

  • Launch the Active Directory Users and Computers console
  • Select Advanced Features from the View menu
  • Right click on an OU and select Properties
  • Look for distinguishedName in the Attribute Editor tab

Importing using PowerShell

The PowerShell script is my own work, written by myself. As always, I’ve included comments to explain the code to help understand it better.

Import AD Users Script

Download the PowerShell script and modify it to suit any changes you may have made to the CSV file and save it with a .ps1 extension.

Make sure you have the CSV file and the script in the same directory to begin with. To run the script simply right-click on it and select “Run with PowerShell”. Launch Active Directory Users and Computers console to check if the import was successful.

Follow Me, Myself and IT on Twitter:

23 thoughts on “Importing Users into Active Directory from a CSV File Using PowerShell

  1. Works great with a small tweak if you use semicolon as separator, which is default in Sweden:
    $ADUsers = Import-csv -Delimiter “;” -Path “.\narcon-users.csv”

  2. Dear Friends
    i need your helping because i\m in bad situation can i import linux users to active directory by powershell scripts

  3. Getting the following error…

    New-ADUser : Directory object not found
    At C:\Users\admin\Downloads\ImportADUsers.ps1:29 char:3

    Checked script and there isn’t anything on that line that suggests there is an error. Line is…

    New-ADUser `

  4. I’m getting this, but the user never generates:

    Processing started (on 05/31/2017 15:22:30):
    ——————————————–
    [INFO] Creating user : eld-bradytj
    ——————————————–

  5. hi, i will recieve this error

    Get-ADUser : Variable: ‘Username’ found in expression: $Username is not defined.
    At line:15 char:6
    + if (Get-ADUser -F {SamAccountName -eq $Username})
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Co
    mmands.GetADUser

    i do a test before of touch my principal server and no work form me

  6. I get the error for each user -AccountPassword : The term ‘-AccountPassword’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or
    if a path was included, verify that the path is correct and try again.

    Please advise

  7. Thank you very much, your code was really useful, I managed to understand it very well and also I was able to add some more fields I needed to add when like “profile path, home Drive”

Leave a comment