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

powershellmicrosoft technologiessoftware & coding更新于 2026/2/14 20:22:17

要将数据附加到 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

相关文章