如何使用 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP