If you are looking for Powershell a script to move AD objects from one location to another location then you are at the right place, in this article we explain how to move multiple AD objects from source to destination O.U with a simple script.
[cmdletbinding()]
param (
[parameter(mandatory=$true)]
$DestinationOU
)
$sourcepath = “OU=Sales Servers,OU=Deployment Servers,DC=CitrixTech,DC=COM”
#We need to import the Active directory Module
Import-Module ActiveDirectory
$Domain = [ADSI]""
$DN=$domain.distinguishedName
$SourcePath = "CN=Servers," + $DN
$Servers = Get-ADServer -Filter * -SearchBase $SourcePath
if(!$Servers)
{
write-host "No Servers are found in O.U"
return
}
foreach ($Server in $Servers)
{
if(!(Move-ADObject $Server -TargetPath $DestinationOU))
{
$Status = "Server Objects Moved successfully"
}
else
{
$Status = "FAILED"
}
$resultobj = New-Object -TypeName PSobject
$resultobj | Add-Member -MemberType NoteProperty -Name ServerName -Value $Server.Name.tostring()
$resultobj | Add-Member -MemberType NoteProperty -Name SourcePath -Value $SourcePath
$resultobj | Add-Member -MemberType NoteProperty -Name DestinationPath -Value $DesitinationOU
$resultobj | Add-Member -MemberType NoteProperty -Name Status -Value $Status
$resultobj
}
How to execute the script
One needs to save this script name as Moveadobjects.ps1 and then run the script as below.
.\Moveadobjects.ps1 -DestinationOU “OU=Retail Servers,OU=Deployment Servers,DC=CitrixTech,DC=COM”
In the above exmples we moved the server objects from Sale O.U to Retail O.U, source O.U is “OU=Sales Servers,OU=Deployment Servers,DC=CitrixTech,DC=COM” and destional O.U is “OU=Retail Servers,OU=Deployment Servers,DC=CitrixTech,DC=COM”
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply