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

ped*_*leo 2 csv powershell

可能听起来很简单,但我无法让它工作......

$file = 'D:\TESTING.csv'

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

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

echo $file
Run Code Online (Sandbox Code Playgroud)

期望的输出:

a b c
- - -
1 2 3
Run Code Online (Sandbox Code Playgroud)

实际产量:

nothing
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 7

就是这条线:

$file = import-csv $file -Header a , b , c | export-csv $file
Run Code Online (Sandbox Code Playgroud)

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

假设您要同时导出数据并将其保留在会话中,您可以执行以下操作:

$filedata = import-csv $file -Header a , b , c  
$filedata | export-csv $file -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

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

Import-CSV $file -Header a , b , c | Tee-Object -Variable $filedata | Export-CSV $file -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)