question

DavidW-8453 avatar image
0 Votes"
DavidW-8453 asked DavidW-8453 commented

find and replace in a file using powershell

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
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It would help if you would point us to the folder and file that you are trying to update so that we could test the command syntax to get it right before replying.

0 Votes 0 ·

the file is : Local State.

located : C:\Users\%username%\AppData\Local\Google\Chrome\User Data

0 Votes 0 ·
MotoX80 avatar image
0 Votes"
MotoX80 answered

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"

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DavidW-8453 avatar image
0 Votes"
DavidW-8453 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered DavidW-8453 commented

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"





capture.jpg (78.3 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Nice work!!!


Sorry... I forgot to mention...

on a FRESH install of Chrome.. the 'Local State' file doesn't have "browser":{"enabled_labs_experiments":["read-later@2"], in it

a fresh 'Local state' file only has '"browser":{ (im not at home to give you the exact statement at the moment)

"browser":{ is what im looking for... to update it to "browser":{"enabled_labs_experiments":["read-later@2"],












0 Votes 0 ·

fresh install of chrome has

"browser":{"last_redirect_origin":""

I need it to update to

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

0 Votes 0 ·

Chrome on my laptop has never had that file updated so if you look at the green highlight in the image, it already has an empty entry for enabled_labs_experiments.

So if you replace...

 "browser":{ 

with ...

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

I will end up with ...

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


That of course is based on the contents of my Local State file. Your contents may vary so you may need to add additional tests to look for existing enabled_labs_experiments entries.

And Chrome might also ignore duplicate enabled_labs_experiments entries, but I would think that you have a higher probability of Chrome generating some error.


0 Votes 0 ·

ok....

so based on my needs.... with looking for

"browser":{"last_redirect_origin":""

what would the script be that you created ? ?


Sorry... im not so good when it comes to scripting in powershell

0 Votes 0 ·
DavidW-8453 avatar image
0 Votes"
DavidW-8453 answered DavidW-8453 commented

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That's going to be a challenge because it contains pipleline characters, and both single and double quotes. If it was one or 2 lines of code that wouldn't be a problem.

To get it parsed correctly, each double quote in the code needs to be 3 double quotes on a command line.

 powershell -command """"Hello""" | write-host"

IAnd if's a single line, good luck making any changes to it. I'd recommend using a .ps1 file.


0 Votes 0 ·

ok!!.... thank you for your help with this!!!

0 Votes 0 ·