none
How can I match 2 local groups with local\domain users?

    Question

  • I have 2 local groups with local and domain users in them. I want to identify which of the users in group1 are not members of group2 and have them added to group2.

    How can I do it in PowerShell?

    Thanks.

    Thursday, March 15, 2012 1:51 PM

Answers

  • While I don't have time to write a complete solution, this can serve as a template to find missing users that are in group1 and not in group2 and add them into group2 by using the Compare-Object cmdlet.

    $Group1 = 'bob','henry','joe','foo','bar'
    $Group2 = 'bob','joe','foo'
    Compare-Object -ReferenceObject $Group1 -DifferenceObject $group2 | Where {
        #This will only filter for missing users on Group2 that can be added to Group2
        $_.SideIndicator -eq "<="    
    } | ForEach {
        Write-Host ("Adding {0} to Group2" -f $_.InputObject)
    }


    Boe Prox

    Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.

    Looking for a script? Check out the Script Repository
    Need a script written for you? Submit a request at the Script Request Page

    • Marked as answer by ASAFKOREN Thursday, March 15, 2012 10:03 PM
    Thursday, March 15, 2012 5:52 PM
    Moderator