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 ?
finance_teacherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steven CarnahanNetwork ManagerCommented:
0
Will SzymkowskiSenior Solution ArchitectCommented:
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.
0
finance_teacherAuthor Commented:
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 ?
0
Will SzymkowskiSenior Solution ArchitectCommented:
If you only want to see the differences then use the following as i have modified the original script.

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

Compare-Object -ReferenceObject $UserGroups -DifferenceObject $CompareGroups

Open in new window


I have tested my original script in my lab with a user that was only part of "Domain Users" (ex. User1) and used another account that is part of many groups 10+ groups (ex. User2).

When you run the original script it will look at both users and compare them the Referencing Object is the object that will be modified if there are any groups that Differencing Object has. As i have stated i have tested this in my lab and it works correctly.

The first sAMAccountName you enter when you run the script is the user that you want to add the groups to. The second sAMAccountname is the user you are comparing against. If the second user has any groups that the first user does not then it will add those groups to the first user.

So in my lab after running this script on User1 they had all 10+ groups added to their AD Account. When originally they were only part of Domain Users.

Will.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2012

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.