I'm trying to figure out the logic to do something like this:
- Query all AD groups in a specific OU
- Query all the users in a specific OU
- Query all the user's group memberships
- If any user belongs to one or more groups in the initial group query, output that information
- If any user belongs to none of the groups in the initial group query, also output that information
I've dug around on this site and found a script that works for the most part, but I'm stuck on how I can compare the user's group membership to the original group query that I'm pulling. It looks like I could use the compare-object cmdlet but the parameters don't seem to include anything that would let me keep track of how many groups the two objects have in common.
The code I found online is below:
$groups = Get-ADGroup -Filter * | where {$_.distinguishedname -like "*,OU=TUNE_TEST_GROUPS,OU=TUNE_TEST,DC=tune,DC=priv"} $users = Get-ADUser -Filter * | where {$_.distinguishedname -like "*,OU=TUNE_TEST_USERS,OU=TUNE_TEST,DC=tune,DC=priv"} foreach ( $User in $Users ) { $userGroups = Get-ADPrincipalGroupMembership $User if ( $userGroups.Count -gt 1 ) { "{0} is a member of the following {1} groups:" -f $User.SamAccountName, $userGroups.Count foreach ( $group in $userGroups ) { "`t{0}" -f $group.Name } } elseif ( $userGroups.Count -lt 1 ) { "{0} is a member of the following {1} groups:" -f $User.SamAccountName, $userGroups.Count foreach ( $group in $userGroups ) { "`t{0}" -f $group.Name } } } The problem with this is that I don't have a way of comparing the user group names to the names of the group query in line 1. I also can't determine that a user belongs to 1 or more groups from that list. I'm not sure if I can use the same count method.