听起来很简单,但我做不到......
$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/