CSV file editing using PowerShell

Viewed 23784

I need to do below using PowerShell scripting.

I have the below data in CSV file. I need to append another prefix to the name according to the subject supplied

Name,Subject,Roll,Div
Mark,Agri,3,Div1,
Tom,CS,6,Div3,
Alex,Aero,9,Div6

Suppose i am supplied the subject as Agri, the name should have prefix as P-. So the same CSV file will be saved and have below content

Name,Subject,Roll,Div
P-Mark,Agri,3,Div1,
Tom,CS,6,Div3,
Alex,Aero,9,Div6

Here's what I have so far:

(Import-Csv E:\Test.csv -Delimiter ',') | ForEach-Object{

    if($_.Subject -match "Agri" )
    {
      $_ = "P-" + "$_";

    }
    else
    {
     $_ = "F-" + "$_";

    }

} | Export-Csv E:\Test.csv -Delimiter ',' 
2 Answers
Related