PowerShell script to copy group membership from one computer to another

When using Active Directory groups, with computers in, to deploy applications in SCCM it can sometimes be useful to copy group membership from one computer to another. This can be useful if a user change computer but is going to continue having the same applications.

The script below will copy the group membership from one computer to another.

To use the script do this:

  1. Copy the script below to a file and save the file to CopyComputerMembership.ps1
  2. Run the script on a computer with the PowerShell module ActiveDirectory installed and make sure that you have access to move the group membership.
  3. Run the script like this: CopyComputerMembership.ps1 -FromComputer <computername> -ToComputer <computername>

Param(
    [Parameter(Mandatory=$True)]
    [String] $FromComputer,
    [Parameter(Mandatory=$True)]
    [String] $ToComputer
)
Process
{
    Import-Module ActiveDirectory
    $ToClient = Get-AdComputer -Identity $ToComputer
    $FromClient = Get-ADComputer -Identity $FromComputer -Properties MemberOf
    Foreach ($Group in $Client.MemberOf)
    {
        $GroupObj = Get-ADGroup -Identity $Group
        Add-ADGroupMember -Identity $GroupObj -Members $ToClient
    }
}

5 Responses to PowerShell script to copy group membership from one computer to another

  1. bren December 10, 2014 at 07:59 #

    This has not worked for me;l
    At C:\Windows\system32\CopyComputerMembership.ps1:11 char:15
    + Foreach ( $Groupin$Client MemberOf )
    + ~
    Missing variable name after foreach.
    At C:\Windows\system32\CopyComputerMembership.ps1:11 char:24
    + Foreach ( $Groupin$Client MemberOf )
    + ~~~~~~~
    Unexpected token ‘$Client’ in expression or statement.
    At C:\Windows\system32\CopyComputerMembership.ps1:11 char:32
    + Foreach ( $Groupin$Client MemberOf )
    + ~~~~~~~~
    Unexpected token ‘MemberOf’ in expression or statement.
    At C:\Windows\system32\CopyComputerMembership.ps1:8 char:2
    + {
    + ~
    Missing closing ‘}’ in statement block.
    At C:\Windows\system32\CopyComputerMembership.ps1:11 char:41
    + Foreach ( $Groupin$Client MemberOf )
    + ~
    Unexpected token ‘)’ in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingVariableNameAfterForeach

  2. Jonathan December 10, 2014 at 12:06 #

    Hi,
    The script was missing spaces between variables. That is why you had some problems running the script. Try coping the script again and run it.
    This sometimes happens when coping the script from the script file on my computer to the blog.
    /Jonathan

  3. Hawkie March 17, 2016 at 23:42 #

    Am I wrong, or shouldnt this:
    Foreach ($Group in $Client.MemberOf)

    Instead be:
    Foreach ($Group in $FromClient.MemberOf)

    • Krozar September 28, 2016 at 16:30 #

      Hawkie, you are correct. Good catch.

  4. Jonathan September 29, 2016 at 09:00 #

    Thanks for pointing this out 🙂

Leave a Reply