Search code examples
powershellcsvcompareobject

Compare-Object and include properties not being compared in output


I'm comparing two CSV files that come from different sources (different column/property names) using the Compare-Object cmdlet. How can I include properties that are in either CSV file in the output without including them in the comparison?

Example CSV data

users1.csv

e-mail-address,name,side
[email protected],Luke,light

users2.csv

e-mail-address,hiredate,hobbies
[email protected],5/2/17,Sabacc

The following gives me a column with the e-mail address and side indicator, but how can I get $Users1.name and $Users2.hiredate without using them in the comparison?

$Users1 = Import-Csv users1.csv
$Users2 = Import-Csv users2.csv

Compare-Object $Users1 $Users2 -Property "E-mail-Address"

I'd like output similar to:

e-mail-address | SideIndicator | name | hiredate
---------------|---------------|------|----------
[email protected]    | <=            | Luke |
[email protected]   | =>            |      | 5/2/17

Solution

  • Add the PassThru parameter to have Compare-Object return all the properties, then use Select-Object to grab the name and hiredate properties:

    Compare-Object $users1 $users2 -Property e-mail-address -PassThru|Select-Object e-mail-address,SideIndicator,name,hiredate