• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Powershell - Comparing groups membership

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

Group1
Group2
Group3

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
0
MilesLogan
Asked:
MilesLogan
  • 2
  • 2
1 Solution
 
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.

http://technet.microsoft.com/en-us/library/ee617193.aspx

http://technet.microsoft.com/en-us/library/ee156812.aspx
0
 
SubsunCommented:
You just want to verify if the members of Group2 & Group3 are also members of Group1.. Am I correct?
0
 
MilesLoganAuthor Commented:
Hi Subsub .. yes
0
 
SubsunCommented:
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"
	$result
	}
	Else{
	"All is well.. :-)"
	}
}

Compare-Member Group1 Group2
Compare-Member Group1 Group3

Open in new window

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

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now