Powershell - Comparing groups membership

I am trying to compare the group members of one group to multiple others.


Is there a way in Powershell for me to see if all the users in Group2,Group3 are members of Group1 ? ideally for it to output the SAmAccountNames of missing accounts that are in Group2 and Group3 that are not in Group1
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

CorinTackNetwork EngineerCommented:
Powershell includes a cmdlet called Compare-Object that you can use to accomplish this. You would need to first import the AD powershell tool and then use the Get-ADGroupMember cmdlet to pull the list of users from Group 2 and Group 3, then compare them to the members in Group 1. See the following for explanations of these cmdlets.


You just want to verify if the members of Group2 & Group3 are also members of Group1.. Am I correct?
MilesLoganAuthor Commented:
Hi Subsub .. yes
Try this and see if it works for you...
Function Compare-Member ($Source,$Group) {
	$DN = (Get-AdGroup $Source).DistinguishedName
	"Working on group $Group"
	$result = Get-ADGroupMember $Group | % { 
	If (!(Get-ADUser $_.SamAccountName -properties memberOf | 
		? {$_.memberOf -Match $DN})){$_.SamAccountName}
	If ($result){
	"Missing Members"
	"All is well.. :-)"

Compare-Member Group1 Group2
Compare-Member Group1 Group3

Open in new window


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
MilesLoganAuthor Commented:
Thank you Subsun !!
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

From novice to tech pro — start learning today.