Change a value in column of a CSV file

18,227

Solution 1

Here is some example code for how you might solve this with a Switch statement

$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

Solution 2

try this

(Import-Csv C:\temp\LastAnalyse.csv | 
    %{$_.'Available Person'=if ($_.Path -eq 'Y') {'1'} elseif ($_.Path -eq 'N') {')'} else {$_.'Available Person'}; $_}) | 
        Export-Csv  C:\temp\LastAnalyse.csv -NoTypeInformation
Share:
18,227

Related videos on Youtube

Paddy
Author by

Paddy

Updated on September 15, 2022

Comments

  • Paddy
    Paddy over 1 year

    I'm trying to find a way to change the value of all the entries in the 'Available Person' column in my csv file using PowerShell.

    If the value is 'Y' it should be changed to '1' and if the value is 'N' it should be changed to ')':

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

    Here's what I've tried:

    $csv=Import-Csv user.csv' | $csv | %{ if($_.'Available Person' -eq "Y") {$_.'Available Person'="1"} } $csv|export-csv user1.csv' -NoTypeInformation
    
  • Esperento57
    Esperento57 over 7 years
    doesnt work, if 'Available Person' = 'PAPY IS A NOOBY' your solution replace by 'PAP1 IS A (OOB1' :)