Copy OU structure from one domain to another domain

Sometimes I have the need to copy the organizational units (OU) structure from one domain to another. This is one way to do it with PowerShell.

Start off by getting all the OUs that is going to be copied from one domain to the other and put them into a text file. Below is one way to do it.

$ou= Get-ADOrganizationalUnit -Filter * -SearchBase “OU=LAB,DC=LAB,DC=SE”; $ou.DistinguishedName > ou.txt

Open the file an do a “search and replace” and change the “DC=LAB,DC=SE” to fit the domain to copy to.

Copy the changed file to the folder C:\temp on a server with the “activedirectory” PowerShell module installed.

When this is done run the following PowerShell script to add the OUs.

# File with OU structure
$ou=Get-Content -Path C:\temp\OU.txt
# Get every line from the file
foreach ($iou in $ou)
    #  Create OU path
    for ($i=1; $i -lt $sou.count; $i++)
        if  ($path-eq"")
            $path=$path + "," + $sou[$i]
    Write-Host "Creating: $name - $path"
    # Create OU structure
    New-ADOrganizationalUnit -Name $name -Path "$path"


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.