powershell - 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
解决方案
如果您真的想打破 - 命令的行,请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
推荐阅读
- c - makefile 的问题,“make: Nothing to be done for 'all'” [C]
- python - 使用 np.isclose() 比较时间向量时出现内存错误
- .net - 更改属性网格集合显示
- ios - 使用不同的单元格创建 UITable
- c++ - Manjaro 上的安装和编译提升
- java - 如何从 Excel 文件中获取 @tag 到 Selenium java 中的 CucumberOptions
- windows - MSYS2:使命令不显示任何内容并且运行时间过长
- node.js - 如何将 PDF 文件响应从 Node js 的后端发送到 Swift 的客户端?
- flutter - Flutter iOS 模拟器
- python-3.x - 在 EC2 实例上安装 numpy 1.21.0