Solved

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

Posted on 2012-04-10
3
636 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 68

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Join & Write a Comment

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now