# Change just one value in csv file

**URL:** https://forums.powershell.org/t/change-just-one-value-in-csv-file/7130
**Category:** PowerShell Help
**Created:** [September 2, 2016, 9:43am UTC](https://forums.powershell.org/t/change-just-one-value-in-csv-file/7130 "2016-09-02T09:43:28Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tony-antony](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tony-antony/32/192_2.png) [@tony-antony](https://forums.powershell.org/u/tony-antony)
#### Post date: [September 2, 2016, 9:43am UTC](https://forums.powershell.org/t/change-just-one-value-in-csv-file/7130/1 "2016-09-02T09:43:28Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)
#### Post date: [September 2, 2016, 10:29am UTC](https://forums.powershell.org/t/change-just-one-value-in-csv-file/7130/2 "2016-09-02T10:29:58Z")

</div>

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
```

---

<div class="post-metadata">

### Author: ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)
#### Post date: [September 2, 2016, 10:36am UTC](https://forums.powershell.org/t/change-just-one-value-in-csv-file/7130/3 "2016-09-02T10:36:07Z")

</div>

```
$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
```

---

<div class="post-metadata">

### Author: ![tony-antony](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tony-antony/32/192_2.png) [@tony-antony](https://forums.powershell.org/u/tony-antony)
#### Post date: [September 2, 2016, 10:37am UTC](https://forums.powershell.org/t/change-just-one-value-in-csv-file/7130/4 "2016-09-02T10:37:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![tony-antony](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tony-antony/32/192_2.png) [@tony-antony](https://forums.powershell.org/u/tony-antony)
#### Post date: [September 2, 2016, 12:40pm UTC](https://forums.powershell.org/t/change-just-one-value-in-csv-file/7130/5 "2016-09-02T12:40:46Z")

</div>

$random commandline, thank you

---

<div class="post-metadata">

### Author: ![tony-antony](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tony-antony/32/192_2.png) [@tony-antony](https://forums.powershell.org/u/tony-antony)
#### Post date: [September 7, 2016, 1:20pm UTC](https://forums.powershell.org/t/change-just-one-value-in-csv-file/7130/6 "2016-09-07T13:20:35Z")

</div>

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!"
}
```

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/change-just-one-value-in-csv-file/7130/7 "2024-05-16T20:40:33Z")

</div>


