Copy group members from one group to another

by james2305 at 2012-12-07 04:24:57


I imported the ActiveDirectory module and trying to copy all the members from one group to another. Could someone tell me where I am going wrong in my command please

Get-ADGroupMember “group1” |  % {Add-ADGroupMember “group2” -Members $}

I will be honest and say I got the command off an internet page and have tried removing the % and the brackets to no avail.

Can someone help please

Many thanks

by Klaas at 2012-12-07 05:49:50
Get-ADGroupMember doesn’t return the actual user objects, so you have to get-aduser first:
Get-ADGroupMember “group1” | Get-ADUser | Foreach-Object {Add-ADGroupMember -Identity “group2” -Members $
by RichardSiddaway at 2012-12-07 11:53:48
Actually you don’t need to get the user at all - James’ code was almost right.

This shows how the membership of one group can be copied to another using the Microsoft cmdlets
$gsource = “GroupUnvlSecA”
$gtarget = "GroupDmlSecA"
Get-ADGroupMember -Identity $gsource |
foreach {
Add-ADGroupMember -Identity $gtarget -Members $($_.DistinguishedName)

The differences is that I use the distinguished name property from the group member object to define the user I’m adding into the group. This will throw an error message if the user already exists in the group but will continue

Thanks a lot RichardSiddaway! This is great: