Replace multiple String characters and quotes in a file - powershell

I am trying to replace an entry in the Google chrome local state file.
Replace
"browser":{"enabled_labs_experiments":[],"last_redirect_origin":""
with
"browser":{"enabled_labs_experiments":["same-site-by-default-cookies#2"],"last_redirect_origin":""
I am using
(Get-content "$env:USERPROFILE\AppData\Local\Google\Chrome\User Data\local state") -replace '"browser":{"enabled_labs_experiments":[],"last_redirect_origin":"', '"browser":{"enabled_labs_experiments":["same-site-by-default-cookies#2"],"last_redirect_origin":""'
I am getting
The regular expression pattern "browser":{"enabled_labs_experiments":[],"last_redirect_origin":" is not valid. At line:1 char:1

You're trying to do ordinary replace like C#'s string.Replace(), but in posh -replace operates on regexes and seems like your literal unfortunately happens to be invalid regex expression. Try with .Replace(), this way:
(Get-content "$env:USERPROFILE\AppData\Local\Google\Chrome\User Data\local state").Replace( `
'"browser":{"enabled_labs_experiments":[],"last_redirect_origin":"', `
'"browser":{"enabled_labs_experiments":["same-site-by-default-cookies#2"],"last_redirect_origin":""')
Note that I added ` at the end of each line, that's just for better formatting.

Related

How do I remove a character from a string in Powershell

I have a string written in XML which approximates to:
60-894-74987
I need a line in powershell that will remove the '-' in the string to leave me with '6089474987'
I've been trying to utilize the -replace function, but it either errors or breaks powershell.
the basic code is:
$serialnumber= $pcinfo[4] #([4]= <value>60-984-74987</value>)
$serialnumber= $serialnumber replace '-',"
I dont get an error with this, powershell gives me >>
Change
$serialnumber= $serialnumber -replace '-',"
to
$serialnumber= $serialnumber -replace '-'
If you're just deleting characters and not replacing them, you don't need explicit empty quotes.
You can simply write as C# fashion below.
$serialnumber = $serialnumber.replace("-","")

Powershell .Replace RegEx

RegEx for Replace is kicking my butt. I am trying find:
value="COM8"/>
in a text file and replace "COM8" with another com port (ie "COM9", "COM13", etc).
(Get-Content 'C:\Path\File.config').Replace('/".*?"', '"COM99"') | Set-Content 'C:\Path\File.config'
Thank you in advance for any help you can provide.
Get-Content produces a list of strings. Replace() is called on each string via member enumeration. Meaning, you're calling the String.Replace() method, not the Regex.Replace() method. The former does only normal string replacements.
Use the -replace operator instead:
(Get-Content 'C:\Path\File.config') -replace '=".*?"', '="COM99"' |
Set-Content 'C:\Path\File.config'

powershell add-content contains ""

I have a question about the syntax in powershell for Add-Content. My problem is, I want to add text into a textfile and that this text contains "" which is not working. For example:
Add-Content -Value "c:\Users\Administrator\Desktop\"foobar.exe""
Now the output should look like this:
c:\Users\Administrator\Desktop\"foobar.exe"
and that does not work because of these "".
Is there a way to get these "" in the -value parameter?
You can do it like this
Add-Content -Value "c:\Users\Administrator\Desktop\`"foobar.exe`"" -Path .\AA.TXT
escaping the inner ""
Or using single quote to enclose the double quote
Add-Content -Value 'c:\Users\Administrator\Desktop\"foobar.exe"' -Path .\AA.TXT
You can use a few different tricks:
Include the doubly quoted items within a singly quoted string. This makes PowerShell ignore variables, too. For instance: 'I Said "Something"'
Include the doubly quoted items with a preceding backtick. Backticks are the escape character in PowerShell, like \ in javascript or C, and this will make the " part of the string.
Use a here document . They start like this: #" and must end with a "# at the start of a line. This will still let you use variables within the quotes, and still let regular double quotes be.

How can I remove CRLF if anywhere between double quotes, using PowerShell?

My text file looks like this.
"MikeCRLF","","","Dell","DevelCRLFCRLFoper"CRLF
"SuCRLFsan","","","Apple","ManagCRLFer"CRLF
Desired result:
"Mike","","","Dell","Developer"LF
"Susan","","","Apple","Manager"LF
I tried this on PowerShell:
"C:\Users\abc\Desktop\1.txt"
(Get-Content $path -Raw).Replace("`r`n","`n") | Set-Content $path -Force
When I do this, I don't get the desired result. Also, I am left with one CRLF at the end. I don't want that either.
Please tell me how to do this using PowerShell v3.
This method avoids checking to see if \r\n is in quotes. Instead, it tries to find the "real" end of line situations and converts those first. Then it just purges the rest.
(Get-Content test.txt -Raw) -replace '([^,]")(\s*\r\n\s*)+("[^,])',"`$1`n`$3" -replace '\r\n',''
I think this should handle most of the stuff you throw at it, but let me know if you find a special case.
edited to fix the replacement string
If you are using the PowerShell Community Extensions, you can use the ConvertTo-UnixLineEnding command e.g.:
ConvertTo-UnixLineEnding C:\users\abc\desktop1.txt -dest desktop1-converted.txt -Enc ascii

Using PowerShell to replace string that contains $ in the string

I am trying to use PowerShell do a simple find and replace. I use template text files and use $ in front of values that need to be changed.
Example:
(Get-Content "D:\test") | Foreach-Object {$_ -replace "`$TBQUAL", "DBO"} | Set-Content "D:\test"
It should find the line OWNER=$TBQUAL and make it look like OWNER=DBO.
I am using the escape in front of $TBQUAL with no luck. To test that it is working if I removed the $ from the front, it would replace TBQUAL and made it look like OWNER=$DBO.
Two things to get this to work:
Use single quotes for your strings so that the $ is not interpreted as the start of a variable to be expanded.
Escape the $ using a backslash "\" so the regular expression parser takes it literally.
For example,
PS C:\> 'Has a $sign in it' -replace 'a \$sign', 'no dollar sign'
Has no dollar sign in it
If you aren't using regular expressions in your replacement, you can do a simple (and fast) replace like this:
Foreach-Object {$_.Replace('$TBQUAL', 'DBO')}