So we have recent come across a situation where we need to replace a line of text in a txt file (config file). We have the following PS code, it opens the file, appears to process the command, closes and saves the file (at least the time stamp has updated).
$old = ' "enabled_labs_experiments": [ ],'
$new = ' "enabled_labs_experiments": [ "disable-direct-write" ],'
(Get-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State – Copy") | ForEach-Object {$_ -replace $old, $new} | Set-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State – Copy"
We do not need the variables but we did it that way instead of inline so that we could more easily see what we are changing.
Basically nothing gets updated in this file. There are other places in this same file that we can update the code and it works perfectly so I have a feeling that it has to do with the [ ] or the : or some other character that PS uses. Unfortunately we cannot omit the : or the [ ] due to that being exactly what we need to change in the file.
The following works perfectly
$old="USA"
$new="en"
(Get-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State") | ForEach-Object {$_ -replace $old, $new} | Set-Content "C:\Users\Jesse\AppData\Local\Google\Ch
rome\User Data\Local State"
Any suggestions are greatly appreciated!!