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:
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.
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:
Follow @emeneye
Import ad users with more attributes( even complex attributes like memberof,homedrive,etc).
Active Directory Manager from adsysnet
download here http://adsysnet.com/downloads.aspx
The above is now a malware site
Thank you very helpful
Will this script work with Server 2012 R2?
awesome. saved my day..!
Thanks
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”
Hi Magnus, thanks for sharing
Hi em.en.eye,
Thank you for sharing. Follow your instruction and it works for me. I would like to add a line ” username could not change a password and password never expire”. Can you help?
Thanks,
Brendan
Dear Friends
i need your helping because i\m in bad situation can i import linux users to active directory by powershell scripts
tnx for this great tutorial! specially those comments in the script!
thank you,good job
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 `
Thank you very much!
I’m getting this, but the user never generates:
Processing started (on 05/31/2017 15:22:30):
——————————————–
[INFO] Creating user : eld-bradytj
——————————————–
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
the same issue coming on my server as well
Me too
Hi,
Thanks you.
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
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”
Life Saver!
Tks SIr, good job!
How can i create a remote user with .csv file and what is the script?