user3911968
user3911968

Reputation: 15

Powershell Import-CSV - How to Select-Object & Rename Specific Columns and Export to NewCSV

I am trying to simply rename the column names from an existing CSV and then export the new one.

I do not want all columns. Only specific. All examples I have seen show how to do all, instead of specific columns.

But I am stuck on this error:

Select-Object : Missing an argument for parameter 'Property'. Specify a parameter of type 'System.Object[]' and try again.

Import-Csv "ORIGINALFILEPATHHERE" | 
Select-Object -Property 
@{name="Dept";expression=$_.'Account'},
@{name="Office";expression=$_.'Location'}| 
Export-Csv -Path "EXPORTPATHHERE"  -NoTypeInformation -UseCulture -Force

Upvotes: 0

Views: 2537

Answers (1)

Dennis
Dennis

Reputation: 1790

If you really like to break the line of the select-command anyway, tell PowerShell to continue on the next by using a backtick (`)...

And I'm sure you like to keep all the current data that you are piping as well while removing the old column names :)

Import-Csv "ORIGINALFILEPATHHERE" | 
Select-Object -Property *, `
@{name='Dept';expression={$_.Account}},
@{name='Office';expression={$_.Location}} | 
Select-Object -Property * -ExcludeProperty Account,Location|
Export-Csv -Path "EXPORTPATHHERE"  -NoTypeInformation -UseCulture -Force

Upvotes: 1

Related Questions