powershell - 如何将标题添加到没有标题的 CSV 中?

标签 powershell csv

听起来很简单,但我做不到......

$file = 'D:\TESTING.csv'

Set-Content $file "1,2,3"

$file = import-csv $file -Header a , b , c | export-csv $file 

echo $file

期望的输出:

a b c
- - -
1 2 3

实际输出:

nothing

最佳答案

就是这一行:

$file = import-csv $file -Header a , b , c | export-csv $file

您正在将数据传送到 export-csv cmdlet。该命令没有输出,因此 $file 将为空。 $file 还包含输出的路径。为什么要将其更改为文件内容?

假设您既要导出数据又要将其保留在 session 中,您可以改为执行以下操作:

$filedata = import-csv $file -Header a , b , c  
$filedata | export-csv $file -NoTypeInformation

您也可以使用 Tee-Object 在一行中完成。

Import-CSV $file -Header a , b , c | Tee-Object -Variable $filedata | Export-CSV $file -NoTypeInformation

关于powershell - 如何将标题添加到没有标题的 CSV 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28546927/

相关文章:

csv - 在两个微服务之间发送文件

c# - 使用 C# 中的 powershell cmdlet 重启 Hyper V 机器

Powershell 到事件

将 SSH key 添加到剪贴板的 Powershell/github 问题

excel - 通过 SSIS 包生成时,CSV 不会产生整数值的格式问题

javascript - 来自 CSV 的 Google 折线图

c# - 使用命令行参数从 C# 执行 PowerShell 脚本

powershell - if($?) { } 是否有 powershell 模式

python - 打开 csv 作为整数列表

java - 数据未使用 Apache Commons 写入 csv