Solved

Powershell - Check two AD groups and show difference

Posted on 2016-09-15
6
46 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 500 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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