Compare-Object does not return multiple values with the same name


  • Hi All,

    I am not a developer. but by researching code and combining examples I Always get quite far, but not this time. I just cannot seem to figure out how to get this to work and kinldy ask your help.

    I want to compare two CSV files and have all common words displayed and send to a 3rd file.

    Here is what I have:

    1st CSV file: BOLlist.csv


    2nd CSV file: Brandlist.csv


    For comparing these files in Powershell I have created the following:

    $BOLlist = Import-Csv -Path "d:\temp\Bollist.csv" | select Brand,Description
    $Brandlist = Import-Csv -Path "d:\temp\Brandlist.csv" -Header "Brand"
    Compare-Object $Bollist $Brandlist -Property "Brand" -passthru -IncludeEqual -ExcludeDifferent | export-csv -Path "d:\temp\output.csv" -NoTypeInformation -encoding UTF8

    The output is: Output.csv:



    What you can see is that Zelda is only in the list one time while it should be two. I found out that the last one in the list is written as last in the output.csv so all entries with thesame name are overwritten. How do I go about fixing this? I think i need a ForEach statement but my devskills are not that high.

    In the end, I'd like to see this result:


    thanks in advance!

    • Edited by Larsvb Friday, April 21, 2017 7:48 AM
    Friday, April 21, 2017 7:47 AM

All replies

  • Start with "help Compare-Object -Full"


    Friday, April 21, 2017 7:59 AM
  • which gives me thesame information as

    but it does not help me in my quest.

    • Edited by Larsvb Friday, April 21, 2017 9:35 AM
    Friday, April 21, 2017 9:34 AM
  • It does exactly what you asked.  It will compare two CSV files and show you all the items that match.

    First read the whole help carefully and try to understand what it does.

    To begin with you are importing the CSV file wrong.  Just import them.

    $BOLlist = Import-Csv d:\temp\Bollist.csv
    $Brandlist = Import-Csv d:\temp\Brandlist.csv

    Also note that there is a bug in Compare-Object when a CSV has only one column and the header has an extra space.


    Friday, April 21, 2017 9:41 AM
  • After fixing your Csv file this works:

    D:\scripts> $Brandlist = Import-Csv Brandlist.csv
    D:\scripts> $BOLlist = Import-Csv Bollist.csv
    D:\scripts> Compare-Object -ReferenceObject $BOLlist -DifferenceObject $Brandlist -Property Brand -IncludeEqual -Exclude
    Different -passthru
    Brand Description SideIndicator
    ----- ----------- -------------
    Zelda Games2      ==
    Mario Father      ==


    Friday, April 21, 2017 9:50 AM
  • If you just wan a simple match:

    D:\scripts> $BOLlist |?{$_.Brand -in $brandlist.Brand}
    Brand Description
    ----- -----------
    Zelda Games2
    Zelda Games
    Mario Father


    Friday, April 21, 2017 9:52 AM
  • This is amazing, I am still trying to understand what the code does but it works. SO I was looking in the wrong direction with trying to use Compare-Object

    thanks very much!

    Friday, April 21, 2017 10:01 AM