find and replace in a file using powershell

David W 41 Reputation points
2021-07-08T00:33:50.833+00:00

I would like to change the following in a 'Local State' chrome config file

from

"browser":{

Change to

"browser":{"enabled_labs_experiments":["read-later@2"],"

using this to replace standard Strings is great... but as you can see.. my needs are a little more complex

im having problems using this following command
powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt

the issue is that it appears that powershell knows only RAW... so the " and @ and : sends it into a spin...

is there a way to tell powershell to ingore those?.... or is there another way I can find & replace using powershell?

(F.A.R.T has the same issue)

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,086 questions
{count} votes

Accepted answer
  1. MotoX80 31,566 Reputation points
    2021-07-08T03:02:25.757+00:00

    This should work. Double check the quotes and brackets to make sure that I got it right.

    $ThisFile = 'c:\temp\Local State'  
    $data = Get-Content $ThisFile   
    if ($data.Contains('read-later@2')) {  
        "Looks like this file was already updated."  
        "Exiting."  
        exit  
    }  
      
    if ($data.Contains('enabled_labs_experiments') -eq $false) {  
        "I didn't find enabled_labs_experiments anywhere."  
        "I will insert after browser."  
        $data = $data.Replace('browser":{','browser":{"enabled_labs_experiments":["read-later@2"],')     
        $data | Out-File -encoding ASCII $ThisFile  
        "I updated $ThisFile"  
        exit  
    }  
      
    if ($data.Contains('enabled_labs_experiments":[]') -eq $false) {  
        "Looks like this file has something different specified for enabled_labs_experiments."  
        "I don't know what to do so I'm exiting."  
        exit  
    }  
      
      
    $data = $data.Replace('enabled_labs_experiments":[]','enabled_labs_experiments":["read-later@2"]')     
    $data | Out-File -encoding ASCII $ThisFile  
    "I updated $ThisFile"  
     
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. David W 41 Reputation points
    2021-07-08T00:45:10.047+00:00

    just to confirm... I cant add this to the end of the 'local state' file it needs to be where it finds it... and then replaces it... or Chrome will consider it a courpt 'local state' file

    0 comments No comments

  2. MotoX80 31,566 Reputation points
    2021-07-08T02:13:12.537+00:00

    Looks like you will need to account for this string...

    "enabled_labs_experiments":[]  
    

    112794-capture.jpg

    I copied the file to C:\temp so that I could test. Save this as a .ps1 file to execute.

    $ThisFile = 'c:\temp\Local State'  
    $data = Get-Content $ThisFile   
    if ($data.Contains('read-later@2')) {  
        "Looks like this file was already updated."  
        "Exiting."  
        exit  
    }  
    if ($data.Contains('enabled_labs_experiments":[]') -eq $false) {  
        "Looks like this file has something different specified for enabled_labs_experiments."  
        "I don't know what to do so I'm exiting."  
        exit  
    }  
      
    $data = $data.Replace('enabled_labs_experiments":[]','enabled_labs_experiments":["read-later@2"]')     
    $data | Out-File -encoding ASCII $ThisFile  
    "I updated $ThisFile"  
    

  3. David W 41 Reputation points
    2021-07-08T03:18:18.697+00:00

    can this be compressed into 1 line ? to run from CMD ?