In this lesson I am going to show you how you can use Powershell to create a script that will import users stored in a CSV file to Active Directory. If you want to download the CSV file you can do so by clicking here. At the bottom of this page you will find a link to download the Powershell script if you would rather not create the script yourself.

Inside of the CSV file we begin with the first row that acts as a header. It allows us to know what information is stored in each column like first / last names, job title, phone / email, description and their destination OU:

In order to import this information we will use the Powershell ISE to write our script.

The first thing we are going to need to do is import the Active Directory module so we can create a user account for the users listed in our CSV file. You can do that with the import-module command:

Next we need to get the path to the CSV file we want to import. We will prompt the user to enter the path by using the Read-Host command:

In order to create an AD user account with Powershell, we need to create a password that is in the SecureString format. We can do that with the following command:

The last thing we need to do before we start creating the user account is import the CSV into a variable with the Import-Csv.

Now that we have the CSV file imported to the variable $users, we can use a ForEach loop to go through each row of the CSV and complete actions (like creating a new account for each row in the CSV). We call each row inside of the CSV a $user so inside of the brackets we will use $user in $users.

We can now use the variable $user.’row heading name’ to reference items. So if we wanted to get the office phone of each row in the CSV, we would call $users.’Office Phone’.

Inside of the ForEach loop, we are going to do three things:

  1. Gather users information (like first name, last name, job title, office phone, email address etc…)
  2. Create the use account with the gather information in step one
  3. Output a message stating we created the account (optional)

After the third step is complete, the ForEach loop will move on to the next row and complete the same actions.

To gather all the required info we will assign the information in each column to a variable.

Notice that we are calling $user.”FIrst Name” to store that info into the $fname variable. Next we are going to create the user account with the New-ADUser command:

Now we are just going to use the echo command so it will be visible each time a new account is created:

And that is it! The whole script can be found below:

Now creating several user accounts in AD can be made much less complicated! Click here to download the Powershell script if you would like… Thanks for reading!



Get Your Free Video Course!

Get the premium video course titled "Build Your Own Windows Server IT Lab" for free by subscribing to our newsletter!

Confirm your email address and we will send you the course!