如何使用 PowerShell 編輯 CSV 檔案?


使用 PowerShell 編輯 CSV 檔案時,你需要使用以下命令。

我們已有 CSV 檔案 output.csv,我們首先匯入此檔案。

$csvfile = Import-csv C:\temp\Outfile.csv

輸出

以下是 CSV 檔案的輸出內容。

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

我們需要更新上述檔案。我們將 EMP_ID ‘3000’城市更改為 MUMBAI。如果按 EMP_Name 更新 城市名稱,則會更新兩行,因為有兩個 Charles,因此我們在此使用 EMP_ID token。

示例

$csv |foreach{if($_.Emp_ID -eq "3000"){$_.City = "Mumbai"}}
$csv |Export-Csv $outfile

輸出

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

更新時間: 2020 年 3 月 26 日

6K+ 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告