Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping quotes in powershell.exe via command prompt

Tags:

powershell

cmd

After viewing this post I am trying to escape quotes, but doing a backslash does not escape html quotes like this: <-- notice it's a right double quote unlike the normal ".

E.g:

Works fine:

powershell -noexit -command $str = \"hello '123' world\"; write-host $str

Does not work:

powershell -noexit -command $str = \"hello '123'” world\"; write-host $str

How can I escape this? Or better how do I escape ALL characters at once to get rid of this hassle!

like image 240
Morten_564834 Avatar asked Nov 08 '13 15:11

Morten_564834


People also ask

How do I escape a quote in PowerShell?

To prevent the substitution of a variable value in a double-quoted string, use the backtick character ( ` ), which is the PowerShell escape character.

How do you escape quotes in CMD?

Escape every double quote " with a caret ^ . If you want other characters with special meaning to the Windows command shell (e.g., < , > , | , & ) to be interpreted as regular characters instead, then escape them with a caret, too.

How do I escape special characters in CMD?

The Windows command-line interpreter uses a caret character ( ^ ) to escape reserved characters that have special meanings (in particular: & , | , ( , ) , < , > , ^ ).

How do you quote a quote in PowerShell?

To include the double quotes inside of the string, you have two options. You can either enclose your string in single quotes or escape the double quotes with a symbol called a backtick. You can see an example of both below of using PowerShell to escape double quotes.


2 Answers

Use a backtick ` to escape your special double quote, so this:

powershell -noexit -command $str = \"hello '123'” world\"; write-host $str

becomes this:

powershell -noexit -command $str = \"hello '123'`” world\"; write-host $str

EDIT:

Option 1. If you prefer using here-strings, you can change the above to powershell -file and run your powershell script file. Use here-strings as much as you want there.

Option 2. If you prefer not to deal with your special quote character inside calling/processing application/script, you can switch to using single quotes instead. In this case you only need to escape your single quotes, by doubling them, like this:

powershell -noexit -command $str = 'hello ''123''” world'; write-host $str

Notice here I did not do anything to your double quote character, and it works just fine.

So your string replace code may become slightly easier to read and maintain, especially if the editor does not display this character correctly (like Powershell console does - it shows a regular double quote instead).

like image 191
Neolisk Avatar answered Sep 20 '22 18:09

Neolisk


Running this from a command prompt (and within PowerShell actually):

powershell -noexit -command { $str = "hello '123' world"; write-host $str }

generates:

hello '123' world

Does this do what you need?

like image 25
Robin Avatar answered Sep 20 '22 18:09

Robin