Adding columns to existing CSV in PowerShell

  Kiến thức lập trình

I’m attempting to take data from a CSV file that already exists and put it into a new CSV with additional columns added through PS.

Existing CSV (firstlast.csv)

First Last
John Doe
Jane Doe

I want to add 2-4 more columns to the CSV with headings that require spaces in them. All the data in each column will be the same though. For example:

NewData.csv

One Street First Last New Address
X Street John Doe Mary St
X Street Jane Doe Mary St

Here is what I’ve tried:

$Name = Import-Csv C:firstlast.csv | Select-Object -Property *

$test = @{'One Street' = 'X street' ; 'First Last' = $Name ; 'New Address' = 'Mary Street'} | Select-Object -Property One Street, New Address
$test | ConvertTo-Csv -NoTypeInformation | Export-Csv C:NewData.csv

New contributor

greenGob19 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT