Solved

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

Posted on 2012-04-10
3
648 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 70

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Introduction to Processes

734 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