Link to home
Start Free TrialLog in
Avatar of markpalinux
markpalinuxFlag for United States of America

asked on

How to add column to CSV file with PowerShell

1.      I want to import a CSV file
2.      Add a column to the data - Just the filename
3.      export into a new file.

$MyCSV = import-csv .\ReportDetails.csv
$MyCSV | export-csv .\ReportWithProcessedDate.csv –NoTypeInformation

So how can I add a new column "#2"  before I export the data to a new csv file?
Avatar of oBdA
oBdA

$CsvFile = .\ReportDetails.csv
Import-Csv -Path $CsvFile |
	Select-Object -Property *, @{n='FileName'; e={[IO.Path]::GetFileName($CsvFile)}} |
	Export-Csv .\ReportWithProcessedDate.csv –NoTypeInformation

Open in new window

SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of 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
ASKER CERTIFIED SOLUTION
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
SOLUTION
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
OK, I now feel the need to add some more comments about this:
* "Add-Member" is dead slow. I try to avoid it whenever possible; basically the only occasion where there's no way around it is if the 'live' object with all of its methods is still required (for example results from a WMI query), because those don't survive a Select-Object.
* "New-Object -TypeName System.IO.Fileinfo" will return a System.IO.Fileinfo; strongly typing $fi is unnecessary and only confusing for a short script like that.
* Neither of the "Add-Member" scripts will work, because Add-Member by default does not return anything; the -PassThru switch is required for the pipeline to work.

@Qlemo:
"better written as", while at the same time replacing properly named arguments with positional ones? I expected better from you ;)
And you have a trailing "w" at the $CsvFile variable for the FileInfo object.

So the Add-Member approach is "best written as"
$CsvFile = '.\ReportDetails.csv'
$FileInfo = New-Object -TypeName System.IO.FileInfo -ArgumentList $CsvFile
Import-Csv -Path $CsvFile | 
	Add-Member -MemberType NoteProperty -Name FileName -Value $FileInfo.Name -PassThru | 
	Export-CSV -Path .\ReportWithProcessedDate.csv –NoTypeInformation

Open in new window

I leave out named parameters occasionally and with intent, if it is obvious what the parameter values mean. But I avoid Add-Member too, so didn't remember the PassThru switch requirement.

About strong typing: I expect different from you :D. You know it is not the same whether you do or omit that. But again, it is not necessary here and bloating, but so is using named parameters.

To put that straight - my own suggestion would be along what oBdA posted first.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- oBdA (https:#a42274726)
-- Jose [MCSE] Ortega C (https:#a42274490)
-- Qlemo (https:#a42274836)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Pber
Experts-Exchange Cleanup Volunteer