Change just one value in csv file

I’m trying to find a way to replace one value in a csv file. This is what my CSV looks like, as you can see, the ‘Available’ consists of Y and N

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

[io.file]::ReadLines($CSV).replace($First.Available,”N”) | Out-File $CSV -Encoding ascii –Force

The problem is

$First.Available
is Y. But when I tell it to change just that one Y to a N, it replaces all Y’s to N’s.

How would I change just the first Y to a N instead of all Y’s to N’s?

The Replace method of an instantiated the .NET Regex class has the option to specify a maximum number of replace operations.

Example:

$lines = Get-Content -Path $CSV -Raw

$pattern = $First.Available
$regEx = New-Object -TypeName 'System.Text.RegularExpressions.Regex' -ArgumentList $pattern
$result = $regEx.Replace($lines, 'N', 1)

$result | Out-File -FilePath $CSV -Encoding ascii -Force
$content = Import-Csv .\path\to\old.csv
If ($content[0].Available -eq 'Y'){$content[0].Available = 'N'}
$content | Export-Csv -Path .\path\to\new.csv -NoTypeInformation

@Daniel Krebs, you’re a genius, thank you very much

$random commandline, thank you

I had everything working, but I did something this morning, and now it’s not working again. I’m not sure what I did.

This is the problem:

I typed in 2000, and it grabbed the correct Core ID (FMD354200000), but as you can see below, it added my computer name to a 1000 core ID.

Branch Number,CoreID,Available
1000,FMD354100000,2UA51427CH
1000,FMD354100001,Yes
1000,FMD354100002,Yes
1000,FMD354100003,Yes
1000,FMD354100004,Yes
2000,FMD354200000,Yes
2000,FMD354200001,Yes
2000,FMD354200002,Yes
2000,FMD354200003,Yes

$Workstation = $env:COMPUTERNAME
$CoreIP = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\CORe\IP"
$CSV = '\\hqfs1\users\tantony\PowerShell\GetCoreID\CoreID.csv'
$ImportCSV = Import-Csv $CSV
$GetBranchNum = Read-Host "Enter branch number and press ENTER: "

if($ImportCSV | Where-Object {$_."Branch Number" -eq $GetBranchNum})
{
    Write-Host -NoNewline "Branch number found in CSV file!`t" -ForegroundColor Green

    if($ImportCSV | Where-Object {$_."Available" -eq "Yes".Trim() -eq "Y" -and $_."Branch Number".Trim() -eq $GetBranchNum})
    {       

        Write-Host "$HowManyLeft Available Core ID found!" -ForegroundColor Green

        $First = $ImportCSV | Where-Object {$_.'Branch Number'.Trim() -eq $GetBranchNum -and $_.Available.Trim() -eq "Yes"} | Select-Object -First 1

        $AddCoreID = $First.CoreID

        New-ItemProperty -path $CoreIP -name "TTable ID" -PropertyType String -Value $AddCoreID -Force | Select-Object "TTable ID" | Format-List

        $lines = Get-Content -Path $CSV -Raw

        $pattern = $First.Available
        
        $regEx = New-Object -TypeName 'System.Text.RegularExpressions.Regex' -ArgumentList $pattern
        
        $result = $regEx.Replace($lines, $Workstation, 1)

        $result | Out-File -FilePath $CSV -Encoding ascii -Force

    }

    else
    {
        Write-Host "No Available Core ID found!" -ForegroundColor Red           
    }
}

else    
{
    Write-Warning "Branch number not found in CSV file!"
}