#Powershell to export AD OU Structure

So I am messing around with a testlab and wanted the OU structure I have in production.  So I wrote this :-D to export the OUs.  The result is a CSV file with two columns Path and OU.  Path is just the OU DN in reverse, and in a “nice” view, so you can sort the OUs.

The script uses .net objects to get domain, forest and DC information.  It uses this to perform a search for all objectcategory that is equal to organizationalUnit.

The script also removes any domain / forest information, so it becomes more generic and can be imported anywhere with out worrying about where it came from.

$CurrentDomain       = [System.DirectoryServices.ActiveDirectory.Domain]::getcurrentdomain()
$DC                  = $CurrentDomain.FindDomainController().name + “:389″ #”localhost:389”
$Forest              = $CurrentDomain.Forest.ToString()
$Forest              = “dc=” + $Forest
$Forest              = $Forest.Replace(“.”,”,dc=”)
$domain              = [ADSI]”LDAP://$dc/$Forest”
$Dom                 = “LDAP://” + $Forest
$Root                = New-Object DirectoryServices.DirectoryEntry $Dom
$searcher            = New-Object DirectoryServices.DirectorySearcher
$searcher.PageSize   = 1000
$searcher.filter     = “(objectcategory=organizationalUnit)”
$searcher.SearchRoot = $root
$objs= $searcher.findall()
$domainDN = $domain.distinguishedName

“Path, OU, Type” | Out-File c:psOUs.csv
ForEach($OU in $Objs){
  $tmpProps = $OU.Properties
  $tmpType = $tmpProps.objectcategory | Out-String
  $tmpType = $tmpType.Trim()
  $tmpType = $tmpType.Substring(0,$tmpType.IndexOf(“,CN=”))
  $tmpType = $tmpType.Replace(“CN=”,””)
  $tmpOU   = $OU.Path
  $tmpOU   = $tmpOU.Replace(“LDAP://”,””)
  $tmpOU   = $tmpOU.Replace(“,$domainDN”,””)
  $tmpPath = $tmpOU.Split(“,”)
  $x=””;For($i=$tmpPath.Length;$i–;$i -le 0){$x+= “” + $tmpPath[$i]}
  $x = $x.replace(“OU=”,””)

  $tmpCSV = [CHAR]34 + $x + [CHAR]34 + “,” + [CHAR]34 + $tmpOU + [CHAR]34 + “,” + $tmpType
  $tmpCSV | Out-File OUs.csv -append

notepad c:psous.csv

Let me know what you think  … Next step is to import them


  1. This line has an error:
    $x=””;For($i=$tmpPath.Length;$i–;$i -le 0){$x+= “” + $tmpPath[$i]}
    “You must provide a value expression following the ‘-‘ operator”

  2. HI.. i tried your script,
    i am getting following error
    please help

    You must provide a value expression on the right-hand side of the ‘-‘ operator.
    At line:12 char:35
    + $x=””;For($i=$tmpPath.Length;$i- <<<< ;$i -le 0){$x+= "" + $tmpPath[$i]}
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx
    + FullyQualifiedErrorId : ExpectedValueExpression

  3. Lloyd Wylie says:

    This will be really helpful if I can get it to work. When I run this in 2012 R2 I get the following error. Can you help?

    At C:\exportorganizationalUnit2CSV.ps1:29 char:36
    + $x=””;For($i=$tmpPath.Length;$i–;$i -le 0){$x+= “” + $tmpPath[$i]}
    + ~
    You must provide a value expression following the ‘–’ operator.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : ExpectedValueExpression

  4. It is working if you replace
    $x=””;For($i=$tmpPath.Length;$i–;$i -le 0){$x+= “” + $tmpPath[$i]}
    $x = “”; For ($i = $tmpPath.Length; $i -ge 0; $i–) { $x += “” + $tmpPath[$i] }

  5. Jirk Hadamek says:

    $x=””; For($i=$tmpPath.Length; $i = $i–1 ; $i -le 0) {$x+= “” + $tmpPath[$i]}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: