How to edit the CSV file using PowerShell?


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

Output

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.

Example

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

Output

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

Updated on: 26-Mar-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements