如何使用 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
廣告