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!
To prevent the substitution of a variable value in a double-quoted string, use the backtick character ( ` ), which is the PowerShell escape character.
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.
The Windows command-line interpreter uses a caret character ( ^ ) to escape reserved characters that have special meanings (in particular: & , | , ( , ) , < , > , ^ ).
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.
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).
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With