The answers provided can work for the task at hand but are not giving the most effective method which is actually letting LDAP do its job instead of having PowerShell perform the filtering.
$groupsFilter = ''
# assuming we have the Group's SamAccountName
# create a filter clause for each group
'group1', 'group2', '...' | ForEach-Object {
$groupsFilter += '(samAccountName={0})' -f $_
}
# We need the user's DistinguishedName
# to perform the Member Attribute filtering
$userDN = (Get-ADUser theUser).DistinguishedName
$filter = -join @(
'(&' # AND, all conditions must be met
'(!member={0})' -f $userDN # The user is NOT a member
'(|' # OR, any of the conditions must be met
$groupsFilter # Follow with the filter for any of the groups
')' # Close OR clause
')' # Close AND clause
)
# Now with the constructed filter we can search for any group
# which's samAccountName is one of those in the array of Groups
# AND the target user IS NOT a Member of
Get-ADGroup -LDAPFilter $filter
If we are looking to find those groups where the user is not a recursive member, in the words, where the user is not a member of the group OR any nested group, we only need to change the (!member=...)
clause to use a LDAP_MATCHING_RULE_IN_CHAIN:
'(!member:1.2.840.113556.1.4.1941:={0})' -f $userDN # The user is NOT a recursive member
To have a reference, the constructed LDAP Filter would look like this:
(&
(!member=CN=someuser,OU=someOU,DC=someDomain)
(|
(samAccountName=group1)
(samAccountName=group2)
(samAccountName=group3)
)
)