Creating User Accounts from a CSV (Comma Separated Value) File
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (3)
Saving Progress...
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lecture, I am going to be showing you how you can create active directory users from a CSV file.
I am going to show you how the CSV file looks like and you can find it in the resources of this lecture called NewUsers.csv.
You can see header fields like First Name, Last Name, Job Title, Office Phone, Email Address, Description, the target Organizational Unit or where they need to be placed within Active Directory and whether or not the account should be Enabled once it's created.
So, we are going to use PowerShell to automatically create all these user accounts. These are just 10 users here but if we were working with 500 or a thousand this will also work.
Log into the IPDC01 server and I have the file already saved into my E:\ drive.
We open Windows PowerShell ISE by clicking on the Windows icon on the bottom left and typing powershell and selecting Windows PowerShell ISE.
And the Windows PowerShell ISE opens up.
Let’s start typing out the script with the following:
# Import the AD Module
Import-Module ActiveDirectory
# Get the path to our target CSV file
$filepath = Read-Host -Prompt "Please enter the path to the CSV file that contains the new user accounts"
# Import the CSV as an array
$users = Import-CSV $filepath
Press the Play button to execute the script.
Note that in the bottom pane we have the script executed and it is asking for the path to the CSV so we type in E:\NewUsers.csv and press the Enter key on your keyboard.
The script finishes executing.
Now we need to do something with that information. Let’s add to our script the following:
# Import the AD Module
Import-Module ActiveDirectory
# Get the path to our target CSV file
$filepath = Read-Host -Prompt "Please enter the path to the CSV file that contains the new user accounts"
# Import the CSV as an array
$users = Import-CSV $filepath
# Complete an action for each user in the CSV file
ForEach ($user in $users) {
# Do this for each user
echo $user.'First Name'
}
Execute the script by clicking the Play icon and enter the CSV path during script execution.
The output of the script shows the First Name column of the CSV file.
Now let’s continue with the script adding the following line now:
# Import the AD Module
Import-Module ActiveDirectory
# Get the path to our target CSV file
$filepath = Read-Host -Prompt "Please enter the path to the CSV file that contains the new user accounts"
# Import the CSV as an array
$users = Import-CSV $filepath
# Complete an action for each user in the CSV file
ForEach ($user in $users) {
# Do this for each user
echo $user.'First Name'
echo $user.Description
}
Execute the script by clicking the Play icon and enter the CSV path during script execution.
We can see that by adding the Description we have after the First Name, the Description from the CSV file.
We can see that so far the script is working as we expect.
So, what we can do now is create a new user account. Let’s type the following code:
# Import the AD Module
Import-Module ActiveDirectory
# Get the path to our target CSV file
$filepath = Read-Host -Prompt "Please enter the path to the CSV file that contains the new user accounts"
# Import the CSV as an array
$users = Import-CSV $filepath
# Complete an action for each user in the CSV file
ForEach ($user in $users) {
# Do this for each user
New-ADUser `
-Name ($user.'First Name' + " " + $user.'Last Name') `
-GivenName $user.'First Name' `
-Surname $user.'Last Name' `
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.
Hello, sorry if I am missing something but I do not see the CSV listed under resources. I downloaded both resources (CreateADUsersFromCSV & CreateADUsersFromCSVExpanded) and found 2 PS scripts but no CSV.
Hi Christian Sanchez
We have added the file to the resources from this video.
Ricardo
Sorry, I want to ask. Is using quotation mark with a single(‘ ‘) or (” “) has the same meaning or different?
Hi Adi Nugroho
You can find more information here:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.3
Ricardo
Ok, thank you 👍
Hi, I did try to import CSV file, and it imported and I am able to echo any names or emails, but I can’t get it working in the foreach loop.
Hi Ke Tu
What is the problem with the For loop? I see you can echo any names or emails, so it seems to be working. Any other error?
Ricardo
Hi, I figured out.
Hi Ricardo,
how does the computer knows it has to create the users in the user OU? what if i wanted to creat a couple group, how would I indicate in the script to do so?
Hi ALWI DELGADO ALAUDIN
It knows because the OU path is specified on the Excel sheet. To create a Group is similar just the commandlet changes from New-ADUser toNew-ADGroup.
Check the following for detailed information:
https://activedirectorypro.com/create-active-directory-security-groups-with-powershell/
Ricardo
I’m running the Script but for some reason I’m getting an Error? Here is the Script: And attached is part of the Error: Please tell me what I’m doing wrong?
Thanks!
Hi Yitzchok Rooz
It seems that the user accounts already exist after reading the error code attached if you ran the script and didn’t see them try refreshing Active Directory Users and Computers to see if they appear. Executing the script again might display the message attached.
Ricardo
Thanks! I refreshed Active Directory Users and Computer and found the User Accounts listed in the Default Users OU, but not in the OU I specified as the distinguishedName in the CSV file (as you can see in the Picture I’m attaching?
Hi Yitzchok Rooz
I see them on the Default Users OU as the string on the text file and the Attribute Editor window. What path should they have been? The current one seems to be correct otherwise the script will error out if the script has a wrong OU.
Sorry, I attached the wrong attachment. The problem I’m having is that the newly created User Accounts are going into the Default Users OU, but the CSV File is pointing to the following distinguishedName:
OU=Domain Users,OU=Server Academy,DC=ad,DC=serveracademy,DC=com
I’ll also attach my CSV File and the PowerShell Script below:
Here is the NewUsers.csv file I’m pointing to in the Script: (I’m attaching it as an .xlsx as I wasn’t able to attach it as a .csv file
Here is the PowerShell Script:
Thanks for the attachments and the screenshot, I see what you mean.
I was doing the same in the lab and got a similar result. I got a similar issue when the script failed but it did create the users in the default Users container like in your case maybe because I got the error on a line of the script before the assignment of the OU.
You can delete the users or restart the lab to try again.
Thanks! I did delete the users in the default Users OU, but running the Script again didn’t resolve the issue. It still placed the newly created Users in the default Users OU, and created the same errors in PowerShell?
Ok, let me check in more detail. I’ll be using your script on Lab: ADUC PowerShell Automation.
I did the test with the users and script attached in previous posts.
The script seems to fail and adds the users in the default container in a disabled state.
The OU path on the user excel file attached is:
“OU=Domain Users,OU=Server Academy,DC=ad,DC=serveracademy,DC=com”
But for the lab, I ran it on I changed it to:
“OU=Domain Users,OU=serveracademy,DC=serveracademy,DC=local”
Also, there’s a 0 at the end, which I changed to True.
It took me some multiple runs to get it right.
Thanks!