重复csv头两次没有“追加”(PowerShell 1.0)

我准备了一个PowerShell脚本来导出CSV格式的系统用户列表。 该脚本可以用单个标题行(顶部的标题行)输出具有Export-csv的用户列表。

但是我的要求是在我的文件中重复两次标题行。 在PowerShell 3.0中使用“Append”很容易实现(例如$ header | out-file $ filepath -Append)我们的服务器环境正在运行PowerShell 1.0。 所以我做不到。 有没有解决办法? 我不能自己手动添加它。

谢谢。

你可以做的是:

  1. 使用Export-Csv写入CSV
  2. Get-Content将内容读回到一个variables中
  3. 附加/预先标题行
  4. 把它全部写回文件

喜欢这个:

 # Path to your file $File = "C:\My\File.csv" # Write the CSV file $Data | Export-Csv $File # Read it back as plain text $CSV = Get-Content $File # Add the header line at the start $CSV = $Header + $CSV # Write it all back to the CSV file $CSV | Set-Content $File