Compare Members of a Group using Where-Object not working

I am trying to compare the members of Group A and Group B. Any user in Group A that is not in Group B must be added to Group B. I cannot get the where-object command working

$a= Get-QADGroupMember -Identity 'Group A' -Indirect -Type 'user' -DontUseDefaultIncludedProperties -ldapfilter '(!(userAccountControl:1.2.840.113556.1.4.803:=2))' -SizeLimit 0| Select-Object samaccountname

$b= Get-QADGroupMember -Identity 'Group B' -Indirect -Type 'user' -DontUseDefaultIncludedProperties -SizeLimit 0 | Select-Object samaccountname

$uniqueusers= $a |where-object {$b -notcontains $_}

foreach ($user in $uniqueusers) {Add-QADGroupMember -Identity 'Group B' -Member $user}

I have a Windows Active Directory module version of this script and it works fine, the issue is the where-object line, it does not return any results. I have also tried "$uniqueusers= $a.samaccountname |where {$b.samaccountname -notcontains $psitem}" and it does not work either.

Parents Reply Children
  • When I troubleshoot the script line by line, I do the following:





    Based from the results above, $uniqueusers.count should be 76. But instead 



    It looks like it is not actually comparing the objects. In the Windows Powershell version of this script, I get the count of the users in $a that are not in B.