?
Solved

Powershell - Check two AD groups and show difference

Posted on 2016-09-15
6
Medium Priority
?
48 Views
Last Modified: 2016-09-16
Hi EE

Can someone help me with a script ? I need to check groupA and groupB and show the difference in users from both.

So if groupA has 50 users , groupB has 40 , for it to export " 10 " or "10 missing accounts"
0
Comment
Question by:MilesLogan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 41800379
Try..
Function Check-GroupDiff ($GroupA,$GroupB){
Compare (Get-AdGroupMember $GroupA -Recursive | 
Select -ExpandProperty SamAccountName) (Get-AdGroupMember $GroupB -Recursive | 
		Select -ExpandProperty SamAccountName) | %{
	If ($_.SideIndicator -eq "=>"){
	 $_ | Select @{N="User";E={$_.InputObject}},@{N="MissingIN";E={$GroupA}}
	 }Else{
	 $_ | Select @{N="User";E={$_.InputObject}},@{N="MissingIN";E={$GroupB}}
	 }
 }
}
Check-GroupDiff HelpdeskAdminGroup DesktopAdminGroup

Open in new window

0
 
LVL 2

Author Comment

by:MilesLogan
ID: 41800811
hi Subsun , thats an awesome function but... what I was hoping to do is just get the total number of users.

If GroupA has 10 users and groupB has 4 users , I need it to show the difference .

so basically for it to output , "6 users missing from groupB "

can you help with this ?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 41801019
Try..
Function Check-GroupDiff ($GroupA,$GroupB){
$Comp = Compare (Get-AdGroupMember $GroupA -Recursive | 
Select -ExpandProperty SamAccountName) (Get-AdGroupMember $GroupB -Recursive | 
		Select -ExpandProperty SamAccountName)
	"$(($Comp | ?{$_.SideIndicator -eq "=>"} | Measure).Count) Users Missing in $GroupA"
	"$(($Comp | ?{$_.SideIndicator -eq "<="} | Measure).Count) Users Missing in $GroupB"
}
Check-GroupDiff HelpdeskAdminGroup DesktopAdminGroup

Open in new window

0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 2

Author Comment

by:MilesLogan
ID: 41801788
Very cool ! thank you Subsun
0
 
LVL 2

Author Comment

by:MilesLogan
ID: 41801791
..
0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 41801792
thank you so much!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question