首页 文章资讯内容详情

如何使用PowerShell将数据附加到CSV文件中?

2026-06-04 1 花语

要将数据附加到CSV文件中,您需要使用在导出到CSV文件时附加参数。

在下面的示例中,我们创建了一个CSV文件

示例

$outfile = "C:\temp\Outfile.csv" $newcsv = {} | Select "EMP_Name","EMP_ID","CITY" | Export-Csv $outfile $csvfile = Import-Csv $outfile $csvfile.Emp_Name = "Charles" $csvfile.EMP_ID = "2000" $csvfile.CITY = "New York" $csvfile | Export-CSV $outfile Import-Csv $outfile

现在我们需要将以下数据附加到现有文件中。因此,首先我们将csv文件导入到名为$csvfile的变量中

$csvfile = Import-Csv $outfile $csvfile.Emp_Name = "James" $csvfile.EMP_ID = "2500" $csvfile.CITY = "Scotland"

将数据插入变量后,我们将使用Append参数附加数据。

$csvfile | Export-CSV $outfile –Append

检查输出:

Import-Csv $outfile

输出结果

EMP_Name EMP_ID CITY -------- ------ ---- Charles 2000 New York James 2500 Scotland