User Account Group Membership Copy

This is the quick snippet to be executed in the context of a Domain Administrator:


# copyGroupMemberships.ps1

$fromIdentity='jesters'
$toIdentity="destinationUsername"
function copyGroupMemberships($fromIdentity,$toIdentity){
   $erroractionpreference='stop'
   try{
    Import-module activedirectory
    $groupNames=(Get-ADPrincipalGroupMembership $fromIdentity).Name
    $groupNames|%{Add-ADGroupMember -Identity "$_" -Members $toIdentity -ea SilentlyContinue}
    $currentMemberships=(Get-ADPrincipalGroupMembership $toIdentity).Name
    write-host "$toIdentity now has these memberships:`r`n---------------------------------`r`n$($currentMemberships|out-string)"
    return $true
   }catch{
    write-warning $_
    return $false
   }
}

copyGroupMemberships $fromIdentity $toIdentity
# membershipcopy.bat

@echo off
cls
rem set /p AdminID=Please Input Admin UserID:
rem set / AdminPassword=Please Input Admin Password:

Set /p CopyFrom=Copy From:
Set /p CopyTo=Copy To:

FOR /f "Tokens=*" %%a in ('dsquery user -samid %CopyTo%') DO Set CopyTo=%%a

IF NOT DEFINED CopyTo (
ECHO Could not find %CopyTo% in AD
GOTO :EOF
)

FOR /f "Tokens=*" %%a in ('dsquery user -samid %CopyFrom%') DO Set CopyFrom=%%a
IF NOT DEFINED CopyFrom (
ECHO Could not find %CopyFrom% in AD
GOTO :EOF
)

ECHO Copying groups from user %CopyFrom% to user %CopyTo%...
ping 127.0.0.1 -n 2 > nul
ECHO ===============================================================================
ECHO Copying groups from user %CopyFrom% to user %CopyTo%...

FOR /f "Tokens=*" %%a in ('dsget user %CopyFrom% -memberof') do (
dsmod GROUP %%a -addmbr %CopyTo% | find /i "dsmod succeeded:"
)
ECHO ===============================================================================
ping 127.0.0.1 -n 2 > nul
echo press any key to continue...
pause > nul
cls
exit
# groupCopy.ps1

import-module activedirectory
Do
{
$fromGroup = Read-Host -Prompt 'Copy From Group:'
$toGroup = Read-Host -Prompt 'Copy To Group:'
Add-ADGroupMember -Identity $toGroup -Members (Get-ADGroupMember -Identity $fromGroup -Recursive)
$flag = Read-Host -Promp 'Press Any Key = exit; C = Continue Copying...'
} while ($flag -match '[Cc]')

Leave a Reply

Your email address will not be published. Required fields are marked *