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


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

在下面的示例中,我们创建了一个 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

更新时间: 26-Mar-2020

39K+ 浏览次数

开启你的 职业生涯

完成课程可以获取认证

立即开始
广告
© . All rights reserved.