Skip to content

a quoted path at the start of a line is an expression, not a command, so the line your tool emits runs under cmd.exe and dies under both PowerShells

args quoting · case

bothhard-erroragentciscriptthird-partyrepro: verifiedexpression-vs-command-mode
Affectspowershell 51, pwsh 7, cmd, windows
Fails asPARSERERROR
Mechanismexpression vs command mode
Safe fixcall operator

Every hook your tool patched fails at once. Exit 1, no output, nothing in the log. The line it emitted looks unimpeachable:

"C:\Users\me\plugin\_hooks\run-hook.cmd" sessionstart

Paste it into cmd.exe and it runs. Under Windows PowerShell 5.1 and pwsh 7 it is a ParserError, and the error blames the argument:

Unexpected token 'sessionstart' in expression or statement.
FullyQualifiedErrorId : UnexpectedToken

The path is fine. The file is there. Nothing is wrong with either.

Point a .cmd at echo GOT=[%1] and send the same line through all three:

line powershell 5.1 pwsh 7 cmd.exe
"<path>" sessionstart ParserError, exit 1 ParserError, exit 1 GOT=[sessionstart], exit 0
cmd /c "<path>" sessionstart exit 0 exit 0 exit 0
& "<path>" sessionstart exit 0 exit 0 n/a

Measure the cmd.exe row from inside a .cmd file. Handing the line to spawnSync as an argv puts another quoting layer in front of it and produces a false negative — that happened while measuring this case, and it is the easy way to get the wrong answer.

PowerShell decides how to read a line from its first token. A leading quoted string selects expression mode: the string is a value, and a bare word cannot follow a value. The parser never gets as far as asking whether the path is executable — the opening quote already settled the question.

The instance is small. The class is not: you do not choose the shell that runs what you emit. A tool that writes a command into a config file is writing for whatever the host decides to dispatch through, and hosts differ — one hands the line to cmd.exe, another to the session shell. The two PowerShell editions install side by side and parse this identically, so the useful question is never which edition the user drives, only whether a PowerShell is anywhere in the chain.

Two fixes, for two different amounts of knowledge.

Terminal window
& "C:\path with spaces\tool.cmd" sessionstart # you know PowerShell parses this
cmd /c "C:\path with spaces\tool.cmd" sessionstart # you do not know who parses this

& is the call operator: it forces command mode. Use it when the line is yours.

cmd /c is the one that survives an unknown dispatcher, because cmd is a command in all three shells and hands the quoted path to the cmd.exe you wanted in the first place. It is not free: read msys-rewrites-slash-args before you reach for it, because a Git Bash hop rewrites that /c into C:/ and the fix becomes the next bug.

Test it across every shell that could be in the chain, not the first one that works. 5.1 is always present, so it is always worth running; pwsh only when installed. A matrix that stops at its first green row passes on your machine and fails on the machine of anyone driving the shell it never reached.

Do not try to escape your way out of it. The quotes are not the problem — they are load-bearing, because the path contains spaces. Stripping them to dodge expression mode trades this failure for a path that tokenizes at the first space. An 8.3 short path also removes the quotes and works, right up until it meets a volume with 8.3 name generation disabled or a directory that has no short name.