# Using powershell to import a csv file for OU's

**URL:** <https://forums.powershell.org/t/using-powershell-to-import-a-csv-file-for-ous/2385>\
**Category:** PowerShell Help\
**Created:** [March 26, 2014, 1:17pm UTC](https://forums.powershell.org/t/using-powershell-to-import-a-csv-file-for-ous/2385 "2014-03-26T13:17:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve-schwersinske](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@steve-schwersinske](https://forums.powershell.org/u/steve-schwersinske)\
**Post date:** [March 26, 2014, 1:17pm UTC](https://forums.powershell.org/t/using-powershell-to-import-a-csv-file-for-ous/2385/1 "2014-03-26T13:17:09Z")

</div>

I’m trying to import a csv file to create OU’s in active directory using powershell. Now I have figured out to set it up to get the file. What I haven’t been able to figure out is the scripting to get the names and create the actual UO’s. I’ve searched the web and everyone has a different setup. I’ve attached a copy of my csv file of what I’m try to setup as Ou’s. This is what i have started but after that I’m lost. Any help would be appreciated.

`
Import-Csv "OUspreadsheet.csv" | ForEach-Object {
-ParentContainer $_."GroupLocation" 
 -SamAccountName $_."Logon Username" 
 -FirstName $_."First Name" 
 -LastName $_."Last Name" 
 -Description $_."Member"`

---

<div class="post-metadata">

**Author:** ![steve-schwersinske](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@steve-schwersinske](https://forums.powershell.org/u/steve-schwersinske)\
**Post date:** [March 27, 2014, 4:34am UTC](https://forums.powershell.org/t/using-powershell-to-import-a-csv-file-for-ous/2385/2 "2014-03-27T04:34:54Z")

</div>

no help on this please?

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [March 27, 2014, 5:18am UTC](https://forums.powershell.org/t/using-powershell-to-import-a-csv-file-for-ous/2385/3 "2014-03-27T05:18:31Z")

</div>

Assuming you have RSAT tools installed, you can use the AD module and do something like this:

# OU.csv

# 

#“Name”,“Path”  
#“MyNewOU1”, “DC=FABRIKAM,DC=COM”  
#“MyNewOU2”, “DC=FABRIKAM,DC=COM”  
#“MyNewOU3”, “DC=FABRIKAM,DC=COM”

Import-Module ActiveDirectory  
Import-CSV C:\Users\rob\desktop\OU.csv | ForEach{  
New-ADOrganizationalUnit -Name $_.Name -Path $_.Path -WhatIf  
}

or simplify it further leveraging the values via pipeline, since the CSV has a “Name” and “Path” matching the switches you can just do this:

Import-CSV C:\Users\rob\desktop\OU.csv | New-ADOrganizationalUnit -WhatIf

either should produce this:

What if: Performing operation “New” on Target “OU=MyNewOU1,DC=FABRIKAM,DC=COM”.  
What if: Performing operation “New” on Target “OU=MyNewOU2,DC=FABRIKAM,DC=COM”.  
What if: Performing operation “New” on Target “OU=MyNewOU3,DC=FABRIKAM,DC=COM”.

You’ll just need to remove the -Whatif when you are ready to make the actual changes.

---

<div class="post-metadata">

**Author:** ![steve-schwersinske](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@steve-schwersinske](https://forums.powershell.org/u/steve-schwersinske)\
**Post date:** [March 27, 2014, 6:55am UTC](https://forums.powershell.org/t/using-powershell-to-import-a-csv-file-for-ous/2385/4 "2014-03-27T06:55:14Z")

</div>

I’m configuring this on Windows server 2012 so not using Rsat tools. Is that going to bring in whats on the spreadsheet?

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [March 28, 2014, 2:55am UTC](https://forums.powershell.org/t/using-powershell-to-import-a-csv-file-for-ous/2385/5 "2014-03-28T02:55:13Z")

</div>

You don’t have to be on a specific server, the previously shared code could be run from any machine with RSAT tools. Otherwise, you would need to use ADSI to create the OU’s:

# OU.csv

# 

#“Name”,“Path”  
#“MyNewOU1”, “DC=FABRIKAM,DC=COM”  
#“MyNewOU2”, “DC=FABRIKAM,DC=COM”  
#“MyNewOU3”, “DC=FABRIKAM,DC=COM”

Import-CSV C:\Users\rob\desktop\OU.csv | ForEach{  
 **$OU = [adsi](“LDAP://{0}” -f $_.Path)  
$CreateOU = $OU.Create(“OrganizationalUnit”,(“OU={0}” -f $_.Name))  
$CreateOU.SetInfo()**  
}

I did not test the above code, so be sure to test the code prior to running against an entire CSV by just running the bolded code with static values.

---

<div class="post-metadata">

**Author:** ![steve-schwersinske](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@steve-schwersinske](https://forums.powershell.org/u/steve-schwersinske)\
**Post date:** [March 28, 2014, 5:00am UTC](https://forums.powershell.org/t/using-powershell-to-import-a-csv-file-for-ous/2385/6 "2014-03-28T05:00:09Z")

</div>

Well I went an built a server 2012 virtual machine and been trying to figure things out from there. Tried running the script with changing where my spread heet is located and got errors. Attached are the errors. I feel so lost on this powershell stuff. I would give up on it if I didn’t need to get it done.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [March 28, 2014, 5:39am UTC](https://forums.powershell.org/t/using-powershell-to-import-a-csv-file-for-ous/2385/7 "2014-03-28T05:39:21Z")

</div>

Did you test creating a single test OU to validate the code? Try running something simple like this:

$OU = [adsi]“LDAP://DC=YourDomain,DC=com"  
$CreateOU = $OU.Create(“OrganizationalUnit”,“OU=Test”)  
$CreateOU.SetInfo()

The rest of the code is just looping through the CSV and replacing static values with variables from the CSV. Also, sometimes when posting code from the internet the quotes get messed up. Above, the code I copied has a different quote in front of LDAP versus the quote after DC=com, so I would just replace all of the quotes in the code inside the editor you are using.

---

<div class="post-metadata">

**Author:** ![steve-schwersinske](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@steve-schwersinske](https://forums.powershell.org/u/steve-schwersinske)\
**Post date:** [March 28, 2014, 6:14am UTC](https://forums.powershell.org/t/using-powershell-to-import-a-csv-file-for-ous/2385/8 "2014-03-28T06:14:48Z")

</div>

Well I tried to do a test but came up with errors again. Less errors though (see attached).

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:52pm UTC](https://forums.powershell.org/t/using-powershell-to-import-a-csv-file-for-ous/2385/9 "2024-05-16T20:52:15Z")

</div>


