首页 > 解决方案 > 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

标签: powershellcsvselect-object

解决方案


如果您真的想打破 - 命令的行,请select使用反引号 ( ) 告诉 PowerShell 继续下一个`...

而且我确定您喜欢在删除旧列名的同时保留所有当前正在传输的数据:)

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

推荐阅读