Author Posts

September 2, 2016 at 3:13 pm

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


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?

September 2, 2016 at 3:59 pm

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


$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

September 2, 2016 at 4:06 pm

$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

September 2, 2016 at 4:07 pm

@daniel Krebs, you're a genius, thank you very much

September 2, 2016 at 6:10 pm

$random commandline, thank you

September 7, 2016 at 6:50 pm

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

$Workstation = $env:COMPUTERNAME
$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


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

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