Link to home
Start Free TrialLog in
Avatar of finance_teacher
finance_teacher

asked on

Active Directory -- user's GROUP membership, compare ?

I need to keep user#1's
current security GROUPS,
but also add any GROUPS
to user#1 that are NOT in
user#1, but are in user#2

How can I find all AD "GROUPS"
that are NOT in user#1,
but are in user#2 ?
Avatar of Steven Carnahan
Steven Carnahan
Flag of United States of America image

I have created the script below and it works perfectly for what you have asked.

All you need to do is when you run the script it will ask for the samaccountname of the user to check (user1) and then ask you to type in the name of the user to compare against (user2). Then it will add any groups from User2 to User1 that User1 does not already have.

Import-Module activedirectory
$User = Read-Host "Type sAMAccountName of user to check"
$CompareUser = Read-Host "Type sAMAccountName of user to compare against"

$UserGroups = Get-ADPrincipalGroupMembership -Identity $User
$CompareGroups = Get-ADPrincipalGroupMembership -Identity $CompareUser

$Compared = Compare-Object -ReferenceObject $UserGroups -DifferenceObject $CompareGroups

ForEach ($Group in $Compared)
    {
        Add-ADGroupMember -Identity $Group.InputObject.DistinguishedName -Members $User -Confirm:$false
    
}

Open in new window


Will.
Avatar of finance_teacher
finance_teacher

ASKER

Will, what do I need to change so it only shows me the differences, it does not add any groups from User2 to User1 that User1 does not already have ?
ASKER CERTIFIED SOLUTION
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial