top of page
Search

How to create an Active Directory OU's, Group's and Users from a CSV file with Powershell​

Updated: Feb 7, 2021

Reddit User Priestx (reddit post) is requesting help to create 3 OU with a group in each OU, and a user in each group.


Pre requirements:

Active Directory Powershell module

Permissions to manage Active Directory


Let's start off by playing around with the CSV file and ForEach loops.


ree

Lets say your CSV files looks like this in Excel









ree

Same file but open with Notepad






ree

I saved the file on my desktop on a test VM, I then use the Import-CSV command to added it to the $CSVFile variable





ree

Let's call the $CSVFile variable and see what we get.







ree

In the CSV file we have 3 Headers:

OU,Group,User


We can call each of the values by


$CSVFile.OU

$CSVFile.Group

$CSVFile.User






ree

Let's now play around with ForEach


You can use ForEach-Object or ForEach








ree

Let's create a ForEach Loop for each of the values.


Again you can use ForEach-Object or ForEach to get the same results













Now that we got our CSV imported and had sometime to play with the ForEach loop lets use powershell to create a new OU.


ree

First Import the Active Directory Powershell module



ree

Let's test out the commandNew-ADOrganizationalUnit and create a test OU.






ree

Look's like the command worked

















ree

Let's also try out the Get-ADOrganizationalUnit and find our new OU.






ree

I ran the New-ADGroup command to create the group




ree

It looks like by default the group was created in the Users OU









ree

I ran the New-ADUser command to create the user




ree

It looks like by default the group was created in the Users OU










Ok lets put everything together.


After playing around with the commands here is the final script.



ree

Run Script as a .ps1


ree

 
 
 

Comments


bottom of page