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
Solved

Powershell - Check two AD groups and show difference

Posted on 2016-09-15
6
40 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
  • 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
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

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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