CSV按标题名称删除列

时间:2016-10-17 11:32:47

标签: powershell csv

我正在开发一个应该删除命名列的PowerShell脚本。有没有办法不选择标题名为“xyz”的列?

我试图通过

获取标题名称
$hnames = $csv | Get-Member -MemberType NoteProperty | foreach {$_.name}

并将结果连接到逗号分隔的字符串中并删除不需要的标题

$new = $hnames -join ","
$new = $new -replace "xyz,", ""

并使用select

导入/导出CSV
Import-CSV $tempfile -Delimiter ";" | Select $new |
    Export-Csv tempynew.csv -Delimiter ";" -Encoding UTF8 -NoTypeInfo   

但它只输出CSV文件中的标题。

2 个答案:

答案 0 :(得分:8)

使用-ExcludeProperty参数代替Select-Object

Import-Csv $tempfile -Delimiter ";" | Select * -ExcludeProperty xyz |
    Export-Csv tempynew.csv -Delimiter ";" -Encoding UTF8 -notypeinfo

答案 1 :(得分:2)

您也可以只指定要包含的列,如下所示:

Import-Csv $tempfile -Delimiter ";" | Select Column1,Column2,Column10 | Export-Csv tempynew.csv -Delimiter ";" -Encoding UTF8 -notypeinfo
相关问题