Quoting quotes in powershell.exe using the command line

After looking at this message, I try to avoid quotes, but performing a backslash does not exclude html quotes, like this: " <- pay attention to the correct double quotation mark, unlike the usual one. "

eg:

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 avoid this? Or better, how can I avoid ALL characters at once to get rid of this problem!

+6
source share
2 answers

Use the back of ` to avoid the special double quote, so that:

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

becomes the following:

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

EDIT:

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

Option 2. If you prefer not to deal with your special quote character inside the application for invoking / processing / script, you can switch to using single quotes. In this case, you only need to avoid single quotes by doubling them, for example:

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

Please note that I did not do anything with your double quote character, and it works fine.

Thus, your line replacement code can be a little easier to read and maintain, especially if the editor does not display this character correctly (for example, the Powershell console - a regular double quote is displayed instead).

+8
source

Running this from the command line (and in PowerShell itself):

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

generates:

 hello '123' world 

Does it do what you need?

0
source

Source: https://habr.com/ru/post/957681/


All Articles