Solved

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

Posted on 2012-04-10
3
640 Views
Last Modified: 2012-04-12
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
0
Comment
Question by:1974Widget
3 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 37827331
Well, I would add two columns, to have both reference and target file names:
compare-object (get-content $sourceFile) (get-content $serverFile) |
  Add-Member NoteProperty SourceFile $sourceFile -PassThru|
  Add-Member NoteProperty TargetFile $serverFile -PassThru |
  export-csv -Path d:\some.csv -notype -append

Open in new window

0
 
LVL 6

Expert Comment

by:slidingfox
ID: 37827380
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
0
 

Author Closing Comment

by:1974Widget
ID: 37840407
Just what I asked for!  Thanks!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question