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.

 

Lets say your CSV files looks like this in Excel








 

Same file but open with Notepad





 

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




 

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






 

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





 

Let's now play around with ForEach


You can use ForEach-Object or ForEach







 

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.


First Import the Active Directory Powershell module


 

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





 

Look's like the command worked
















 

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





 

I ran the New-ADGroup command to create the group




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








 

I ran the New-ADUser command to create the user




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.




 

Run Script as a .ps1



1,340 views0 comments

Recent Posts

See All
bottom of page