How to edit the CSV file using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To edit the CSV file using PowerShell, you need to use the below commands.

We already have the CSV file output.csv, we will import this file first.

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


Below is the output of the CSV file.

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

We need to update the above file. We will change the CITY of the EMP_ID ‘3000’ to MUMBAI. If we update the CITY name by the EMP_Name, it will update two rows as there two Charles, therefore we will use the EMP_ID token here.


$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
Updated on 26-Mar-2020 07:32:48