CSV file editing using PowerShell

I wasn't able to pipe this together - might just be ignorance on my part about powershell syntax. I was able to do it with the following using two lines:

($csv = Import-Csv E:\Test.csv -Delimiter ',') | ForEach {
    if ($_.Subject -match "Agri") {
        $_.Name = 'P-' + $_.Name
    } else {
        $_.Name = 'F-' + $_.Name
    }

}
$csv | Export-Csv E:\Test.csv -Delimiter ',' -NoType

Notice:

$_ = "P-" + $_;

Becomes

$_.Name = "P-" + $_.Name;

It seems to me the use of paren's in the following clause.

($csv = Import-Csv E:\Test.csv -Delimiter ',')

will assign to $csv the output of Import-Csv PRIOR to the ForEach processing and therefore the ForEach effects are completely lost. Not sure what you purpose is in using those paren's.

Tags:

Csv

Powershell