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

    Question

  • 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

    Brand,Description
    Zelda,Games2
    Zelda,Games
    Mario,Father
    Princess,Mother
    Peach,Daughter
    Toad,Son

    2nd CSV file: Brandlist.csv

    Brand
    Zelda
    Mario

    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:

    "Brand","Description","SideIndicator"
    "Zelda","Games","=="
    "Mario","Father","=="

     

    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:

    "Brand","Description","SideIndicator"
    "Zelda","Games","=="
    "Zelda","Games2","=="
    "Mario","Father","=="

    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
    Moderator
  • which gives me thesame information as https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/compare-object?f=255&MSPPError=-2147217396

    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
    Moderator
  • 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
    Moderator
  • 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
    Moderator
  • 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