如何使用PowerShell 2的export-csv附加文件? [英] How can I append files using export-csv for PowerShell 2?

查看:135
本文介绍了如何使用PowerShell 2的export-csv附加文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$filesremoved | export-csv -Path E:\Code\powershell\logs\filesremoved.txt -NoTypeInformation

我也尝试过

$filesremoved | export-csv -Path E:\Code\powershell\logs\filesremoved.txt -NoTypeInformation -NoClobber

该文件似乎每次都被覆盖.有没有办法继续向文件添加内容?

The file appears to be overwritten every time. Is there a way to keep adding content to the file?

我遇到错误

Export-Csv : A parameter cannot be found that matches parameter name 'Append'.

推荐答案

Export-Csv-Append参数在PowerShell 3.0之前不存在.

The -Append parameter of Export-Csv doesn't exist until PowerShell 3.0.

在PowerShell 2.0中解决此问题的一种方法是导入现有CSV,创建一些新行,追加两个集合并再次导出.例如,假设test.csv:

One way to work around it in PowerShell 2.0 is to import the existing CSV, create some new rows, append the two collections, and export again. For example, suppose test.csv:

"A","B","C"
"A1","B1","C1"
"A2","B2","C2"

您可以使用以下脚本在此CSV文件中追加一些行:

You could append some rows to this CSV file using a script like this:

$rows = [Object[]] (Import-Csv "test.csv")
$addRows = 3..5 | ForEach-Object {
  New-Object PSObject -Property @{
    "A" = "A{0}" -f $_
    "B" = "B{0}" -f $_
    "C" = "C{0}" -f $_
  }
}
$rows + $addRows | Export-Csv "test2.csv" -NoTypeInformation

运行此脚本,test2.csv的内容将为:

Run this script and the content of test2.csv will be:

"A","B","C"
"A1","B1","C1"
"A2","B2","C2"
"A3","B3","C3"
"A4","B4","C4"
"A5","B5","C5"

这篇关于如何使用PowerShell 2的export-csv附加文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆