ciso
ciso

Reputation: 3050

How to escape PowerShell double quotes from a .bat file

I'm trying to replace all double quotes in a file (temp1.txt) with two double quotes using this PowerShell command, run from a bat file in Windows 7:

powershell -Command "(gc c:\temp\temp1.txt) -replace '\"', '\"\"' | Out-File -encoding UTF8 c:\temp\temp2.txt"

I keep getting the error:

'Out-File' is not recognized as an internal or external command.

When I change the command to replace the letter "a" with the letter "b", it works fine like this:

powershell -Command "(gc c:\temp\temp1.txt) -replace 'a', 'b' | Out-File -encoding UTF8 c:\temp\temp2.txt"

I need to escape the double quote since the entire powershell -Command is within a double quoted string. How do you escape the double quote?

Upvotes: 5

Views: 3614

Answers (3)

js2010
js2010

Reputation: 27606

Here's a geeky answer. Use the ascii code for doublequote instead, which is 34, and avoid quoting issues. Doublequotes are a special cmd.exe character and so is the pipe.

powershell "(gc temp1.txt) -replace [char]34,([char]34+[char]34) | set-content temp1.txt"

Upvotes: 1

Emacs User
Emacs User

Reputation: 1475

The escape character is grave-accent for PowerShell. Try this instead:

powershell -Command "(gc c:\temp\temp1.txt) -replace `", `"`" | Out-File -encoding UTF8 c:\temp\temp2.txt"

Upvotes: 0

briantist
briantist

Reputation: 47872

Hm, here you need to escape the " on the command line, inside a double quoted string. From my testing, the only thing that seems to work is quadruple double quotes """" inside the quoted parameter:

powershell.exe -command "echo '""""X""""'"

So then your command line should be:

powershell -Command "(gc c:\temp\temp1.txt) -replace '""""', '""""""""' | Out-File -encoding UTF8 c:\temp\temp2.txt"

There is another way to handle this with PowerShell, assuming you don't want to just put these commands in a file and call it that way: use -EncodedCommand. This lets you base64 encode your entire command or script and pass it as a single parameter on the command line.

So here's your original command:

(gc c:\temp\temp1.txt) -replace '"', '""' | Out-File -encoding UTF8 c:\temp\temp2.txt

Here's a script to encode it:

$c = @"
(gc c:\temp\temp1.txt) -replace '"', '""' | Out-File -encoding UTF8 c:\temp\temp2.txt
"@
$b = [System.Text.Encoding]::Unicode.GetBytes($c)
$e = [System.Convert]::ToBase64String($b)

$e now contains:

KABnAGMAIABjADoAXAB0AGUAbQBwAFwAdABlAG0AcAAxAC4AdAB4AHQAKQAgAC0AcgBlAHAAbABhAGMAZQAgACcAIgAnACwAIAAnACIAIgAnACAAfAAgAE8AdQB0AC0ARgBpAGwAZQAgAC0AZQBuAGMAbwBkAGkAbgBnACAAVQBUAEYAOAAgAGMAOgBcAHQAZQBtAHAAXAB0AGUAbQBwADIALgB0AHgAdAA=

So your new command line can be:

powershell.exe -encodedCommand KABnAGMAIABjADoAXAB0AGUAbQBwAFwAdABlAG0AcAAxAC4AdAB4AHQAKQAgAC0AcgBlAHAAbABhAGMAZQAgACcAIgAnACwAIAAnACIAIgAnACAAfAAgAE8AdQB0AC0ARgBpAGwAZQAgAC0AZQBuAGMAbwBkAGkAbgBnACAAVQBUAEYAOAAgAGMAOgBcAHQAZQBtAHAAXAB0AGUAbQBwADIALgB0AHgAdAA=

There is no need to worry about escaping anything.

Upvotes: 15

Related Questions