Link to home
Start Free TrialLog in
Avatar of 1974Widget
1974Widget

asked on

How to add a column to default output of export-csv

Is there a way to do add a second column to the CSV in the following code snip?  I'm putting this in a loop that goes through multiple files.  I need another column so I know what file I need to look at when I get a get diff, and I can make it into a table in Excel for easy viewing.

I need the output to be one file, and am going to add Dmitri's funtion from http://dmitrysotnikov.wordpress.com/2010/01/19/export-csv-append/.  

I'm comparing one master set of source files against files from different servers, which is why I need to path as a column.

compare-object -referenceobject $(get-content $sourceFile) -differenceobject $(get-content $serverFile) | export-csv -Path d:\some.csv -notype -append
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Give the code below a try.


$comparison = compare-object -referenceobject $(get-content $sourceFile) -differenceobject $(get-content $serverFile)

$comparison | % {

    $obj = New-Object -type PSObject
    $obj | Add-Member -membertype NoteProperty -name InputObject -Value $_.InputObject
    $obj | Add-Member -membertype NoteProperty -name SideIndicator -Value $_.SideIndicator
    $obj | Add-Member -membertype NoteProperty -name Path -Value $source

    return $obj
   
}

$obj | export-csv -Path d:\some.csv -notype -append
Avatar of 1974Widget
1974Widget

ASKER

Just what I asked for!  Thanks!