Copy AD OU Structure To Another AD Location

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.

Export Active Directory Users data to a CSV file

Here is a old script that I write down during a AD migration, to export users data from Active Directory. Just copy and paste on the notepad and save it as .ps1

#START

#*********************************************************************************************
#
# Export AD users data
#
# Written by João Dias
# Date 10/04/2017
#
# Version: 1.0
#
#*********************************************************************************************

#Define location
$path = Split-Path -parent “c:\temp\*.*”

#Define OU location
$ou = ‘OU=users,DC=domain,DC=local’

#Create a variable for the date stamp in the log file
$LogDate = get-date -f yyyyMMddhhmm

#Define CSV and log file location variables
$csvfile = $path + “\Users_$logDate.csv”

#import the ActiveDirectory Module
Import-Module ActiveDirectory

$AllADUsers = Get-ADUser -Filter * -SearchBase $ou

$AllADUsers |
Select-Object @{Label = “First Name”;Expression = {$_.GivenName}},
@{Label = “Last Name”;Expression = {$_.Surname}},
@{Label = “Display Name”;Expression = {$_.DisplayName}},
@{Label = “Logon Name”;Expression = {$_.sAMAccountName}},
@{Label = “Full address”;Expression = {$_.StreetAddress}},
@{Label = “City”;Expression = {$_.City}},
@{Label = “State”;Expression = {$_.st}},
@{Label = “Post Code”;Expression = {$_.PostalCode}},
@{Label = “Country/Region”;Expression = {if (($_.Country -eq ‘GB’) ) {‘United Kingdom’} Else {”}}},
@{Label = “Job Title”;Expression = {$_.Title}},
@{Label = “Company”;Expression = {$_.Company}},
@{Label = “Directorate”;Expression = {$_.Description}},
@{Label = “Department”;Expression = {$_.Department}},
@{Label = “Office”;Expression = {$_.OfficeName}},
@{Label = “Phone”;Expression = {$_.telephoneNumber}},
@{Label = “Email”;Expression = {$_.Mail}},
@{Label = “Manager”;Expression = {%{(Get-AdUser $_.Manager -server $ADServer -Properties DisplayName).DisplayName}}},
@{Label = “Account Status”;Expression = {if (($_.Enabled -eq ‘TRUE’) ) {‘Enabled’} Else {‘Disabled’}}}, # the ‘if statement# replaces $_.Enabled
@{Label = “Last LogOn Date”;Expression = {$_.lastlogondate}} |

#Export CSV report

Export-Csv -Path $csvfile -NoTypeInformation

#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.