Copy all users from one group to another

Just because I always have to search for this script, and Always need to test the script to verify integrity, here’s the script that WORKS.
You only have to replace the value of $sourcegroup and $targetgroup.

Cheers!

<# 
.SYNOPSIS
     Clone-Group
.DESCRIPTION
     Clones group Members from Source to Target
.NOTES
     Have both Source and Target group Distinguished Name at hand
.LINK
 
#> 

#Set Source and Target Group Distinguished Name

$sourceGroup = [ADSI]"LDAP://CN=MYGROUPNAME,OU=MYCHILDOU,OU=MYPARENTOU,DC=CONTOSO,DC=LOCAL"
$targetGroup = [ADSI]"LDAP://CN=MYGROUPNAME2,OU=MYCHILDOU2,OU=MYPARENTOU,DC=CONTOSO,DC=LOCAL"
"Source Group: $($sourceGroup.samAccountName)"
"Target Group: $($targetGroup.samAccountName)" 
"`nCloning Source Group to TargetGroup`n"   

#get Source members
foreach ($member in $sourceGroup.Member)
{
    Try
    {        
        "Adding Member: $member"        
        $targetGroup.add("LDAP://$($member)")
    }    
    Catch    
    {        
        Write-Host "Error performing add action" -Fore DarkRed
    }
}

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x