
The PowerShell script below can be used to find users that are common to two Active Directory groups.
You just need to change the names of the groups to suit your needs (where “finance” and “managers” are mentioned below).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
# Add the Active Directory PowerShell modules import-module activedirectory; # Get the members of the first group $group1 = get-adgroupmember "finance"; # Get the members of the second group $group2 = get-adgroupmember "managers"; # Loop through each user in Group 1 foreach ($group1user in $group1){ # For each user in Group 1, loop through every user in Group 2 looking for a match foreach ($group2user in $group2){ # If a match is found if ($group1user.samaccountname -eq $group2user.samaccountname) { write-host $group1user.name "is in both groups."; } } } |
The results should look like this:

UPDATE 28/05/2019
I’m starting to go back over some of my older scripts (like this one) and update them with more efficient methods. The script below achieves the same effect in a fraction of the processing time.
|
# Get the members of the first group $group1 = get-adgroupmember "finance"; # Get the members of the second group $group2 = get-adgroupmember "managers; # Compare the two groups on the 'name' property and return only those in both groups compare-object -referenceobject $group1 -differenceobject $group2 -property name -includeequal | where {$_.sideindicator -eq "=="} | select name; |
