Calling it from a shell
Every example on this page makes the same call. headless-run starts C:\Tools\My Tool\tool.exe with GREETING set to hello world, and tool.exe receives these eight arguments:
hello worldsay "hi"--verbose/quiet--100%a&bC:\Log files\
Where the example has a second line, it runs C:\Scripts\job.ps1 -Name "nightly run" -DryRun, and the script, which declares param([string] $Name, [switch] $DryRun), receives $Name as nightly run and $DryRun set.
The first -- ends the launcher’s options. The fifth argument, a second --, is part of the child’s command line and reaches tool.exe like any other argument, since nothing after the first -- is parsed by the launcher. Each shell below passes both of them through unchanged.
scripts/test-shell-examples.ps1 runs each example block on this page exactly as written, with C:\Tools and C:\Scripts pointed at a temporary folder, and checks what tool.exe and job.ps1 received against the lists above. scripts/test.ps1, which CI runs, includes it for cmd, PowerShell 7, Windows PowerShell 5.1, Git Bash, a shortcut, Node, Python and C#, skipping a caller the machine lacks with a note. -Wsl and -TaskScheduler add the WSL and Task Scheduler examples, which were checked on Windows 11 with WSL 2 Ubuntu.
To check a call of your own, put --echo after headless-run and run it the same way: the launcher starts nothing and shows the arguments it received and the exact child command line, in a window when the caller has no console, where you can also edit the line and watch it split again. See Checking what a caller passes. For an argument list of your own, the quoting playground writes the line for each caller below, and reads a line back into arguments.
cmd.exe prompt
Section titled “cmd.exe prompt”start "" /wait headless-run --env "GREETING=hello world" -- "C:\Tools\My Tool\tool.exe" "hello world" "say \"hi\"" --verbose /quiet -- 100% "a&b" "C:\Log files\\"start "" /wait headless-run -- C:\Scripts\job.ps1 -Name "nightly run" -DryRun- The prompt does not wait for a Windows-subsystem program, so without
start "" /waitit shows the next prompt at once and%ERRORLEVEL%is not the job’s exit code. The empty""isstart’s window title, which it would otherwise take from the first quoted argument. &,|,<,>and^arecmdoperators outside quotes: quote the argument, as in"a&b", or escape the character, as ina^&b.cmddoes not know\": it sees the"in"say \"hi\""as the end of a quoted part, sohiis outside quotes forcmd, thoughtool.exereads one argument. That is harmless here, but an operator such as&in that part would split the command.%NAME%expands whenNAMEis set; an unset one, and a lone%as in100%, stay as typed at the prompt.- A trailing backslash before a closing quote is doubled, as in
"C:\Log files\\", since\"would otherwise be read as a literal quote.
.cmd or .bat file
Section titled “.cmd or .bat file”headless-run --env "GREETING=hello world" -- "C:\Tools\My Tool\tool.exe" "hello world" "say \"hi\"" --verbose /quiet -- 100%% "a&b" "C:\Log files\\"headless-run -- C:\Scripts\job.ps1 -Name "nightly run" -DryRunA batch file waits for each command and sets %ERRORLEVEL%, so start /wait is not needed. The prompt’s rules apply, except that % is doubled to %% for a literal %, and an unset %NAME% becomes empty.
PowerShell 7
Section titled “PowerShell 7”headless-run --env 'GREETING=hello world' -- 'C:\Tools\My Tool\tool.exe' 'hello world' 'say "hi"' --verbose /quiet -- 100% 'a&b' 'C:\Log files\' | Out-Hostheadless-run -- C:\Scripts\job.ps1 -Name 'nightly run' -DryRun | Out-Host- PowerShell does not wait for a Windows-subsystem program unless its output is piped, so
| Out-Hostmakes it wait and set$LASTEXITCODE. - Assigning the output is not piping it:
$out = headless-run -- ...returns at once, leaves$outempty and$LASTEXITCODEunchanged, and the child’s writes to the closed output fail. Pipe it on, as in$out = headless-run -- ... | Write-Output, to wait, capture the output and set$LASTEXITCODE. - Single quotes keep
$,`and"literal. PowerShell 7.3 and later quote each argument for the program by Windows’ rules ($PSNativeCommandArgumentPassingisWindows, which treatsheadless-run.exeasStandard), so an embedded"and a trailing\need no escaping. --and arguments that start with-or/reach a native program unchanged.- After the stop-parsing token
--%, the rest of the line goes to the program as typed: single quotes are no longer quotes, and%NAME%expands instead of$name. - With
$PSNativeCommandArgumentPassing = 'Legacy', PowerShell 7 behaves like Windows PowerShell 5.1 below.
Windows PowerShell 5.1
Section titled “Windows PowerShell 5.1”headless-run --env 'GREETING=hello world' -- 'C:\Tools\My Tool\tool.exe' 'hello world' 'say \"hi\"' --verbose /quiet -- 100% 'a&b' 'C:\Log files\\' | Out-Hostheadless-run -- C:\Scripts\job.ps1 -Name 'nightly run' -DryRun | Out-HostWindows PowerShell 5.1 wraps an argument with a space in quotes without escaping what is inside it. So an embedded " is written \", or tool.exe receives say hi, and a trailing backslash in an argument with a space is doubled, or tool.exe receives C:\Log files". | Out-Host makes it wait, as in PowerShell 7.
Git Bash
Section titled “Git Bash”MSYS_NO_PATHCONV=1 headless-run --env 'GREETING=hello world' -- 'C:\Tools\My Tool\tool.exe' 'hello world' 'say "hi"' --verbose /quiet -- 100% 'a&b' 'C:\Log files\'MSYS_NO_PATHCONV=1 headless-run -- 'C:\Scripts\job.ps1' -Name 'nightly run' -DryRunGit Bash rewrites an argument that looks like a POSIX path into a Windows path before it starts a Windows program: without MSYS_NO_PATHCONV=1, /quiet reaches tool.exe as quiet under the Git install folder, such as C:/Program Files/Git/quiet. MSYS_NO_PATHCONV=1 turns that off for the command, so paths are written in their Windows form. To keep the conversion for other arguments, write a leading // instead, as in //quiet, or list the prefixes to leave alone in MSYS2_ARG_CONV_EXCL. Bash waits for the launcher and sets $?.
headless-run.exe --env 'GREETING=hello world' -- 'C:\Tools\My Tool\tool.exe' 'hello world' 'say "hi"' --verbose /quiet -- 100% 'a&b' 'C:\Log files\'headless-run.exe -- 'C:\Scripts\job.ps1' -Name 'nightly run' -DryRun- A Windows program is called by its
.exename. It is found onPATHwhen WSL appends the WindowsPATH, as it does by default. - WSL quotes each argument by Windows’ rules when it builds the command line, as PowerShell 7 does, and it waits for the launcher.
- The program and every path the child reads are Windows paths: a Linux path such as
/mnt/c/Tools/My Tool/tool.exemakes the launcher fail with 125.wslpath -wconverts one, as in"$(wslpath -w ~/job.sh)".
Task Scheduler
Section titled “Task Scheduler”$tool = New-ScheduledTaskAction -Execute 'C:\Tools\headless-run.exe' -Argument '--env "GREETING=hello world" -- "C:\Tools\My Tool\tool.exe" "hello world" "say \"hi\"" --verbose /quiet -- 100% a&b "C:\Log files\\"'$job = New-ScheduledTaskAction -Execute 'C:\Tools\headless-run.exe' -Argument '-- C:\Scripts\job.ps1 -Name "nightly run" -DryRun'Register-ScheduledTask -TaskName 'headless-run example' -Action $tool, $job- The
-Argumentstring, which is the Add arguments field of the task’s action and theArgumentselement of its XML, is the raw command line after the program, so it follows Windows’ rules alone:\"for an embedded quote, a doubled backslash before a closing quote, and no shell operators. - Task Scheduler expands
%NAME%in it when it runs the task, so%USERNAME%reaches the child as the user’s name. A lone%, as in100%, stays. schtasks /create /trtakes the program and its arguments as one string, written insidecmd’s quotes with\"for each embedded quote. It refuses a string longer than 261 characters, and one close to that length can be stored cut short with no error: on Windows 11 a string of exactly 261 characters lost its last 8. The call above does not fit, so useRegister-ScheduledTaskor a task XML file instead.
Shortcut
Section titled “Shortcut”$shell = New-Object -ComObject WScript.Shell$tool = $shell.CreateShortcut('C:\Tools\tool.lnk')$tool.TargetPath = 'C:\Tools\headless-run.exe'$tool.Arguments = '--env "GREETING=hello world" -- "C:\Tools\My Tool\tool.exe" "hello world" "say \"hi\"" --verbose /quiet -- 100% a&b "C:\Log files\\"'$tool.Save()$job = $shell.CreateShortcut('C:\Scripts\job.lnk')$job.TargetPath = 'C:\Tools\headless-run.exe'$job.Arguments = '-- C:\Scripts\job.ps1 -Name "nightly run" -DryRun'$job.Save()Invoke-Item -Path 'C:\Tools\tool.lnk', 'C:\Scripts\job.lnk'- A shortcut’s arguments, the part of its Target field after the program, are the raw command line after the program, as in a scheduled task:
\"for an embedded quote, a doubled backslash before a closing quote, and no shell operators.Invoke-Itemopens a shortcut as a double-click does. - Windows expands
%NAME%in the arguments when it opens the shortcut, so%USERNAME%reaches the child as the user’s name. An unset%NAME%and a lone%, as in100%, stay as written. - A shortcut holds at most 1,023 characters of arguments.
CreateShortcutcuts a longer string to that length with no error, so a long call belongs in a.cmdfile that the shortcut runs throughheadless-run.
Run dialog
Section titled “Run dialog”Type the whole line into the Run dialog (Win+R) and press Enter:
C:\Tools\headless-run.exe --env "GREETING=hello world" -- "C:\Tools\My Tool\tool.exe" "hello world" "say \"hi\"" --verbose /quiet -- 100% a&b "C:\Log files\\"- The Run dialog passes the line the way a shortcut does: the same raw command line,
\"for an embedded quote, a doubled backslash before a closing quote, and no shell operators, soa&barrives as one argument. - It expands
%NAME%too:%USERNAME%reaches the child as the user’s name, while an unset%NAME%and the lone%in100%stay as written. - Its field holds at most 259 characters. A longer line is cut there with no error, so put a long call in a
.cmdfile or a shortcut.
This example was checked by hand on Windows 11, since the test script cannot type into the Run dialog. Run from another folder, the line delivered the eight arguments listed above and the GREETING variable, with %USERNAME% and %NO_SUCH_VAR% appended to check the expansion.
These start headless-run with an argument list, and each runtime quotes the list by Windows’ rules, so the arguments are written as plain strings.
import { spawnSync } from 'node:child_process';
spawnSync('headless-run', ['--env', 'GREETING=hello world', '--', 'C:\\Tools\\My Tool\\tool.exe', 'hello world', 'say "hi"', '--verbose', '/quiet', '--', '100%', 'a&b', 'C:\\Log files\\']);import subprocess
subprocess.run(['headless-run', '--env', 'GREETING=hello world', '--', r'C:\Tools\My Tool\tool.exe', 'hello world', 'say "hi"', '--verbose', '/quiet', '--', '100%', 'a&b', 'C:\\Log files\\'])using System.Diagnostics;
var start = new ProcessStartInfo("headless-run"){ ArgumentList = { "--env", "GREETING=hello world", "--", @"C:\Tools\My Tool\tool.exe", "hello world", "say \"hi\"", "--verbose", "/quiet", "--", "100%", "a&b", @"C:\Log files\" },};using var process = Process.Start(start)!;process.WaitForExit();- Node’s
windowsVerbatimArguments: truepasses the strings joined by spaces without quoting them, so each one must then be quoted by hand, as on the Task Scheduler line. - Python’s
subprocess.list2cmdlineshows the command line a list becomes. - .NET’s
ProcessStartInfo.Arguments, a single string, is passed as written, whileArgumentListquotes each item.
Pitfalls
Section titled “Pitfalls”- The launcher’s
--: everything after the first--belongs to the child, including another--. No shell above removes either one. - Waiting: a
cmdprompt and PowerShell return before a Windows-subsystem program ends; usestart "" /waitor pipe the output, as above. In PowerShell, assigning the output to a variable is not enough; pipe it on with| Write-Output. %: doubled in a batch file, expanded at a prompt when the variable is set, and expanded by Task Scheduler and a shortcut.- Embedded quotes:
\"on every Windows command line, and in Windows PowerShell 5.1 even inside a single-quoted string; PowerShell 7, Git Bash, WSL and the code callers quote for you. - A trailing backslash before a closing quote: doubled wherever you write the command line yourself (
cmd, Windows PowerShell 5.1, Task Scheduler, a shortcut). - Path conversion in Git Bash:
/quietand other slash-led arguments become Windows paths unlessMSYS_NO_PATHCONV=1is set. schtasks /tr: at most 261 characters, and a string near that length can lose its end with no error.