Compare two variables and find values unique to variable1

Viewed 118

Hey quite stumped here... if someone could help me out that would be amazing!

What I am trying to achieve is exporting two lists of email addresses from Office365. Then compare the two and output only email addresses that are unique to the first export.

In theory the two exports would like something like this:

$export1= "user1@onmicrosoft.com","user2@onmicrosoft.com","user3@onmicrosoft.com"
$export2= "user1@onmicrosoft.com","user2@onmicrosoft.com"

In this case my desired output would be "user3@onmicrosoft.com" because that is unique value among the two exports.

I can actually do this with the following lines:

$unique = $export1 | ?{$export2 -notcontains $_}
$unique

$unique
user3@onmicrosoft.com

However this is not the issue. The issue is when using the actual variables exported from Office365, this does not work correctly.

I am not sure if its because the headers of the columns are different upon export or something else.

Anyways I will just show you, these are my two exports:

$userlist1 = Get-TeamChannelUser -GroupId "HIDDEN" -DisplayName "TEST Private Channel" | `
 Select-Object @{N="EmailAddress";Expression={$_.User}}

$userlist2 = Get-MsolGroupMember -GroupObjectId "HIDDEN" | `
 Select-Object @{N="EmailAddress";Expression={$_.Emailaddress}}

Using the Select-Object @blahblah to make sure the column names are lined up. Otherwise the variables would contain two columns with different names, User and EmailAddress. (Happy to suggest another way to do this / if this is important, have tried without just makes things worse:/)

This is the output of $userlist 1 & 2 (might look weird because privacy :p)

$userlist1
EmailAddress      
------------                   
SAS@domain
Sam@domain
Joe@domain

&

#userlist2
EmailAddress      
------------                   
Sam@domain

So the Sam@domain value is the duplicate, so we want that gone and to only show the other values...

Then run together the output is:

$userlist1
$userlist2

EmailAddress      
------------                   
SAS@domain
Sam@domain
Joe@domain
Sam@domain

OK... so Everything looks good to me.

So when I run it through the command Why??? does it not output correctly:

$unique = $userlist1 | ?{$userlist2 -notcontains $_}
$unique

EmailAddress                   
------------                   
SAS@domain
Sam@domain
Joe@domain   

I don't understand haha, please help Thanks!

Also if anything doesn't make sense please let me know.

Sam

1 Answers

Expanding the property EmailAddress is one way to make it works in your case:

$userlist1 = Get-TeamChannelUser -GroupId "HIDDEN" -DisplayName "TEST Private Channel" | `
 Select-Object @{N="EmailAddress";Expression={$_.User}} | select -ExpandProperty EmailAddress
$userlist2 = Get-MsolGroupMember -GroupObjectId "HIDDEN" | `
 Select-Object @{N="EmailAddress";Expression={$_.Emailaddress}} | select -ExpandProperty EmailAddress

$unique = $export1 | ?{$export2 -notcontains $_}
$unique

But why ? If fact, if you look at the output that you showed:

$userlist1
EmailAddress      
------------                   
SAS@domain
Sam@domain
Joe@domain

You can see there is a property "EmailAddress". So you are not comparing a collection of string but a collection of objects.

As suggested by Dilly B in the comment, you should prefer the "Select -Unique". So this one will work without expanding your collection

$export1 + $export2 | Select -Unique
Related