5

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?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
ciso
  • 2,887
  • 6
  • 33
  • 58

3 Answers3

14

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
briantist
  • 45,546
  • 6
  • 82
  • 127
  • 2
    I hate that I only just learned about this. Do you know how much time I've spent double- and quad-escaping quotes and running and re-running batch scripts trying to get everything *just* right? Thank you – gregmac Dec 18 '19 at 03:27
1

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"
js2010
  • 23,033
  • 6
  • 64
  • 66
0

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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Emacs User
  • 1,457
  • 1
  • 12
  • 19
  • 1
    @ChrisGciso This won't work because while the backtick is the escape character within powershell, you need to escape the `"` from the command prompt so that it can be passed to powershell. – briantist Jul 22 '15 at 23:58
  • @ChrisGciso just checked again. You are right. It works on cygwin shell and other *nix shells. All I had to do was double the double quotes in cmd shell. – Emacs User Jul 23 '15 at 00:17