Change Format of Export-CSV output in powershell

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Øyvind Kallstad Øyvind Kallstad 2 years ago.

  • Author
    Posts
  • #20916
    Profile photo of Karunanidhi Mishra
    Karunanidhi Mishra
    Participant

    Hello everyone,

    The below script works well for comparing two CSV files and exports output ,

    #Comparing the PrePatchReport & PostPatchReport
    $file1 =Import-Csv D:\List1.csv
    $file2=Import-Csv D:\List2.csv
    # get list of CSV properties
    $props1 = $file1 | gm -MemberType NoteProperty | select -expand Name | sort
    $props2 = $file2 | gm -MemberType NoteProperty | select -expand Name | sort
    # first check that properties match
    if(Compare-Object $props1 $props2)
    {
    throw "Properties are not the same! [$props1] [$props2]"
    }
    # pass properties list to Compare-Object
    else
    {
    $CR=Compare-Object -ReferenceObject ($file1) -DifferenceObject ($file2)
    $CR|Export-Csv D:\Results.csv -Append -NoTypeInformation
    }

    List1.csv

    KBNumber
    2112212
    5656666
    2211000
    8897991

    List2.csv

    KBNumber
    2112212
    5656666
    2211000

    and output is:

    Results.csv

    "InputObject","SideIndicator"
    "@{KBNumber=8897991}","<=" "@{KBNumber=7665756}","<=" "@{KBNumber=9837129}","<=" "@{KBNumber=1212111}","<=" Kindly help me out in ways to improve the format of my output. Expected Format is: Results.csv KBNumber 8897991 7665756 9837129 1212111 Thanks in advance...

  • #20917
    Profile photo of Øyvind Kallstad
    Øyvind Kallstad
    Participant

    Try replacing your last line

    $CR|Export-Csv D:\Results.csv -Append -NoTypeInformation

    with

    $CR | Select-Object -ExpandProperty KBNumber | Export-Csv D:\Results.csv -Append -NoTypeInformation

You must be logged in to reply to this topic.