Adding/removing members from another forest or domain to groups in Active Directory:

Example of powershell script:
Write-Host “Loading the Quest.ActiveRoles.ADManagement powershell snap-in”
if ( (Get-PSSnapin -Name Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue) -eq $null )
Add-PsSnapin Quest.ActiveRoles.ADManagement

Write-Host “”
$rootOU = “DC=mydomain,DC=local”
$date = Get-Date -Format ddMMyyyy
$log = “.\Update-CVS-GroupMembership-$date.txt”
$startscript = Get-Date
$totalgrp = 0
$nceoldgrp = 0
$changes = 0
$KO = 0
Write-Host “”
Write-Host “————————————————————————————————”
Write-Host “”
Get-QADGroup -SearchRoot $rootOU | %{
$members = $null
$groupname = $_.Samaccountname
$members = Get-QADGroupMember -Identity $_.DN -Type group -Name MII_*
if ($members -ne $null) {
foreach ($member in $members) {
$GroupToReplace = $member.Samaccountname
$GroupToFind    = $GroupToReplace -replace (“MII_”,””)
$GroupExist = $null

$GroupExist = Get-QADGroup -SearchRoot “OU=Groups,DC=mydomain,DC=local” -SearchScope OneLevel -SamAccountName $GroupToFind

If ($GroupExist -ne $null)
Add-QADGroupMember -Identity $groupname -Member $GroupExist.DN -proxy
# Remove-QADGroupMember -Identity $groupname -Member $member.DN -proxy
Write-Output “Modification – $GroupToReplace has been replaced by $GroupToFind in the $groupname” | Out-File $log -Append
Write-Output “Error – $groupname unchanged… $GroupToReplace has not a matching group as $GroupToFind”  | Out-File $log -Append
Write-Host “”
Write-Host “–STATISTICS–” -BackgroundColor Blue -ForegroundColor White
Write-host “TOTAL “$totalgrp” total groups parsed” -BackgroundColor Yellow -ForegroundColor Black
Write-host “TOTAL “$nceoldgrp” total old MII_xxx groups found” -BackgroundColor Yellow -ForegroundColor Black
Write-host “TOTAL “$changes” total groups changed successfully” -BackgroundColor Yellow -ForegroundColor Black
Write-host “TOTAL “$KO” total groups with no matching” -BackgroundColor Yellow -ForegroundColor Black
Write-Host “”
#Start-Sleep 5
Write-Host “——————-”
Write-Host “– End of Script –”
Write-Host “——————-”
Write-Host “”
$stopscript = Get-Date
Write-Host “Has started at” $startscript -BackgroundColor Gray -ForegroundColor Black
Write-Host “Had finished at” $stopscript -BackgroundColor Gray -ForegroundColor Black
Write-Host “TIME SPENT:” (New-TimeSpan -Start $startscript -End $stopscript).hours “Hours” (New-TimeSpan -Start $startscript -End $stopscript).minutes “Minutes” (New-TimeSpan -Start $startscript -End $stopscript).seconds “Seconds” -BackgroundColor Green -ForegroundColor Black
Write-Host “”
Write-Host “”