GC -replace command has problem with " sign

Hi,

I’m new here and can’t say that I’m anywhere near to call myself an expert in Powershell. The commands I use I have found on Google :slight_smile: But usually I achieve what I want in the end.

The problem I have now and can’t find a solution for is that I’m trying to replace a text-string in a bunch of files with this command
powershell -Command “(gc file.txt) -replace ‘Server List=licenseserver’, ‘Server List=“licenseserver”’ | Out-File -encoding ASCII file.txt”
The problem is that the output is Server List= licenseserver"" rather than the desired Server List=“licenseserver”. Obviously it has a problem with the quote sign but I can’t find a way around it. Is it doable?

Hi,
I'm new here as well and just trying to help out.
To use double-quotes (") within a a section started with ", you need to add a second double-quote, ie "". I have updated your code below to help.
powershell -Command "(gc file.txt) -replace 'Server List=licenseserver', 'Server List=""licenseserver""' | Out-File -encoding ASCII file.txt"
I hope this helps.

Thanks, but the result is only that the space beteeen = and l is erased. So it get Server List=licenseserver""

If you’re not in powershell, you’d be better off there instead of calling powershell.exe. If you need to call powershell.exe from another shell/application, then I recommend still getting the syntax sorted out in powershell before taking and using it in this manner. Another important point, a lot of times when you copy code from the internet, the quotes or other characters can be converted into similar but different characters. I make it a habit to replace any single or double quotes at a minimum out of code retrieved from a web page. If you have the choice to download the source vs copying from a page - do that instead.

Now to your issue posted, this command achieves your desired result in powershell. For the sake of others, I added sample code to create the file.

@'
somestring
Server List=licenseserver
anotherstring
'@ |Out-File file.txt -Encoding ascii

(gc file.txt) -replace 'Server List=licenseserver', 'Server List="licenseserver"' |
    Out-File -encoding ASCII file.txt

New file contents:

get-content .\file.txt

somestring
Server List="licenseserver"
anotherstring

If you are indeed calling it from another environment, please add those details if you need assistance making this code work there.

If you are calling this code from PowerShell, then you must escape inner like-quotes in PowerShell and CMD shells. Escaping prevents early closure of outer quotes. If you are calling from CMD only, then you only need to escape inner like-quotes in CMD.
# From CMD

powershell -Command “(gc file.txt) -replace ‘Server List=licenseserver’, ‘Server List="licenseserver"’ | Out-File -encoding ASCII file.txt”

From PowerShell

powershell -Command “(gc file.txt) -replace ‘Server List=licenseserver’, ‘Server List="“licenseserver"”’ | Out-File -encoding ASCII file.txt”
The reason for the double escape in PowerShell is PowerShell will parse the entire command string first. So each "" is parsed as ". Then it is passed to CMD where the remaining " is parsed as ".

Simple as that! It worked, thanks!

I’m using Powershell.exe. I’ve been using the same script to replace other strings lots of times before without a problem. It was just this one there I needed to add " in the text that caused me problem.

So what you did was remove Powershell -command and the leading and trailing " since it’s not needed from within Powershell and that automatically resolved the problem that RichGardener42 was on to?

Now I’ve learned something new. Thanks!

 

[quote quote=249092]If you are calling this code from PowerShell, then you must escape inner like-quotes in PowerShell and CMD shells. Escaping prevents early closure of outer quotes. If you are calling from CMD only, then you only need to escape inner like-quotes in CMD.

# From CMD

powershell -Command "(gc file.txt) -replace 'Server List=licenseserver', 'Server List=\"licenseserver\"' | Out-File -encoding ASCII file.txt"

From PowerShell

powershell -Command “(gc file.txt) -replace ‘Server List=licenseserver’, ‘Server List="“licenseserver"”’ | Out-File -encoding ASCII file.txt”

The reason for the double escape in PowerShell is PowerShell will parse the entire command string first. So each “” is parsed as . Then it is passed to CMD where the remaining " is parsed as .

[/quote]
Cool! Thanks, that did work too!

Learned another new thing :slight_smile: