Copy group members from one group to another

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Brian Edison Brian Edison 1 year, 3 months ago.

  • Author
    Posts
  • #6226

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

    Hi

    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

    James

    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

  • #34051
    Profile photo of Brian Edison
    Brian Edison
    Participant

    Thanks a lot RichardSiddaway! This is great:

You must be logged in to reply to this topic.