There's no good reason to use Start-Process
in your case (see this GitHub docs issue for guidance on when Start-Process
is and isn't appropriate).
Instead, use direct execution, which is not only synchronous and runs in the same console window (for console applications such as wsl.exe
), but also allows you to directly capture or redirect output.
In doing so, you'll also be bypassing the longstanding Start-Process
bug you mention (GitHub issue #5576), because PowerShell double-quotes arguments passed to external programs as needed behind the scenes (namely if they contain spaces).
$exe, $exeArgs = $args # split argument array into exe and pass-thru args
wsl.exe -e $exe $exeArgs # execute synchronously, via wsl -e
Note that you then also don't need to embed escaped "
in your PowerShell CLI call:
powershell -noprofile -executionpolicy bypass -file sh.ps1 bash -c "echo Hello World"
However, if your arguments truly needed embedded "
, you'd run into another longstanding PowerShell bug that affects only direct invocation of external programs, necessitating manual escaping with \
- see this answer.
If you do need to use Start-Process
- e.g. if you want the process to run in a new (console) window - you'll have to provide embedded quoting around those $args
elements that need it, along the following lines:
$quotedArgs = foreach ($arg in $args) {
if ($arg -notmatch '[ "]') { $arg }
else { # must double-quote
'"{0}"' -f ($arg -replace '"', '\"' -replace '\\$', '\\')
}
}
$proc = Start-Process -FilePath "wsl.exe" -ArgumentList $quotedArgs -PassThru
$proc | Wait-Process