0

I have scoured the escape rules for PS but I cannot wrap my head around why the only work some of the time. For example, if I am passing a String argument to a cmdlet, it seems like all bets are off.

PowerShell 'echo "Hello there"'

I would expect this to yield an output of: Hello there, but instead PowerShell completely ignores the double quotes and no matter which escape sequence I use, it refuses to acknowledge that they even exist in the String. Of course, the above is just an example. It doesn't matter which cmdlet I'm using, whenever a String is passed, all escape rules seemingly flat out do not apply. The same goes for passing a String to Node.JS, through PowerShell:

node -p 'console.log("Hello")'

This throws an error, once again because PowerShell decides to throw away my double-quotes and pretend they're not there.

Interestingly enough, if I swap the quotes around, like this:

node -p "console.log('Hello')"

It produces the correct result, though any double-quotes inside the String immediately break it. It's so bizarre that the escape rules appear not to apply to String arguments. Am I misunderstanding the rules? Is there any way to use double-quotes inside single-quoted String arguments that PS won't completely ignore?

Thanks for any insight.

broment
  • 76
  • 6
  • Does this answer your question: [PowerShell stripping double quotes from command line arguments](https://stackoverflow.com/a/59681993/1701026)? – iRon Mar 08 '20 at 18:14
  • In short: Sadly, embedded `"` chars. must _manually_ be `\ `-escaped in order to be passed through to external programs - see [the linked answer](https://stackoverflow.com/a/59036879/45375). – mklement0 Mar 09 '20 at 02:18

1 Answers1

0

This link has most of the rules i use: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7

backticks are used to cancel out variable interpolation:

"`$var" == $var

Preserving double quotes in a double quoted string: "As they say, ""live and learn."""

If you have specific examples we can dig into those.

midacts
  • 196
  • 6