This was made by MALEK Ahmed and change by João Dias, in order to copy a AD structure to a new AD location. Just copy and paste on the notepad and save it as .ps1

 

#START

#*********************************************************************************************
#
# Copy AD OU Structure To Another Location
#
# Written by João Dias
# Date 10/04/2017
#
# Version: 1.0
#
#*********************************************************************************************

#Configuration of the Source and the Destination
$sourceOU = “OU=mutegaold,DC=domain,DC=local”
$destinationOU = “OU=muteganew,DC=newdomain,DC=local”

#AD Path
$adPath= “LDAP://” + $destinationOU

#Importing AD module
import-module activedirectory
#Copying the OU’s to the destination
$objDomain=New-Object System.DirectoryServices.DirectoryEntry($adPath)
$ObjSearch=New-Object System.DirectoryServices.DirectorySearcher($ObjDomain)
[array] $OUs = @()
$OUs = dsquery * $sourceOU -Filter “(objectCategory=organizationalUnit)” -limit 0
$OUsorted = $OUs | sort-object { $_.Length}
for ($k=0; $k -le $OUsorted.Count -1; $k++)
{
$OUtoCreate = ($OUsorted[$k] -replace $sourceOU,$destinationOU).ToString()
$OUSearch = ($OUtoCreate -replace ‘”‘,””).ToString()
$ObjSearch.Filter = “(&(objectCategory=organizationalUnit)(distinguishedName=”+ $OUSearch + “))”
$allSearchResult = $ObjSearch.FindAll()
if ($allSearchResult.Count -eq 1)
{
“No changes were done on = ” + $OUtoCreate
}
else
{
dsadd ou $OUtoCreate
“OU Creation = ” + $OUtoCreate
}
}

#END

The script is provided “AS IS” without warranty of any kind. We disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Mutega IT, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Mutega IT has been advised of the possibility of such damages.

Leave a Reply