Move user between domain's in the same forest PowerShell
This week I try to write PowerShell script for moving users between domain's in the same forest.At first I try to use ADMT 3.2 ,in my test environment the ADMT install on a member server.After several testing I try the RTFM approach (read the faking Manuel).From this approach I learn that if you want to script ADMT it must be install on a Domain Controller!
Below is the Code in PowerShell that do the work enjoy
Below is the Code in PowerShell that do the work enjoy
100 function GetDomainnamefromobjectDN([string]$objDN){
101   $str = $objDN.Split(",")
102   for ($i=$str.length-1 ;$i -gt -1 ;$i--)
103   {if ($str[$i].substring(0,2) -like "dc")
104   {if($strDC)
105   {$strDC = $str[$i].Split("=")[1] + "." + $strDC}
106   else
107   {$strDC = $str[$i].Split("=")[1]}
108   }
109   }
110   return $strDC
111   }
112
113 $Tou = "CN=Users,DC=London,DC=England,DC=CO,DC=UK"
114 $usrDN = "CN=Doron Zilber,CN=Users,DC=England,DC=CO,DC=UK"
115 $TargetRID = $null
116 $SourceRID = $null
117 $objForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
118 $DomainList = @($objForest.Domains | Select-Object Name,RidRoleOwner)
119
120 foreach ($dom in $DomainList)
121   {if ($dom.name -eq (GetDomainnamefromobjectDN($Tou)))
122   {$TargetRID = $dom.RidRoleOwner}
123   if ($dom.name -eq (GetDomainnamefromobjectDN($usrDN)))
124   {$SourceRID = $dom.RidRoleOwner}
125   if ($TargetRID -and $SourceRID)
127   {break}
129   }
130
131 $objUsr = [ADSI]"LDAP://$SourceRID/$usrDN"
132 $MoveToOU = [ADSI]"LDAP://$TargetRID/$Tou"
133
134 # ## Command to Do the actual move
135 $objUsr.PSBase.moveto($MoveToOU)
136