更改CSV文件列中的值

Pad*_*ddy 2 csv powershell

我正在尝试使用PowerShell找到一种方法来更改csv文件中"可用人员"列中所有条目的值.

如果值为'Y',则应将其更改为'1',如果值为'N',则应将其更改为')':

Branch Number,      CoreID,     Available Person,      Workstation
8002,           FMD354800200,   Y,
8002,           FMD354800201,   Y,
8002,           FMD354800202,   N,
8002,           FMD354800203,   N,
8002,           FMD354800204,   Y,

这是我尝试过的:

$csv=Import-Csv user.csv' | $csv | %{ if($_.'Available Person' -eq "Y") {$_.'Available Person'="1"} } $csv|export-csv user1.csv' -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

Ben*_*enH 7

下面是一些示例代码,说明如何使用Switch语句解决此问题

$ImportedCSV = Import-CSV C:\user.csv
$NewCSV = Foreach ($Entry in $ImportedCsv) {
    Switch ($Entry."Available Person") {
        Y {$Entry."Available Person" = "1"}
        N {$Entry."Available Person" = ")"}
        default {Write-Error "$($Entry."Branch Number") has unexpected value for Available Person"}
    }
    $Entry
}
$NewCSV | Export-CSV C:\user1.csv -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)