-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a conceptual help topic about calling external programs (native applications) #5152
Comments
|
True: what matters is whether the token starts with a quote, which in turn only works if the whole token is quoted. It is for this reason that compound string arguments composed of quoted and unquoted tokens are best avoided in PowerShell - the fact that a quoted first token invariably becomes its own argument is counterintuitive and invites confusion: # OK, but only because the first token is unquoted.
C:\'Program Files'\nodejs\node.exe
# NOT OK: becomes string literal *plus separate argument*, because the first token is *quoted*
'C:\Program Files'\nodejs\node.exe
# ALSO NOT OK, despite &, for the same reason.
& 'C:\Program Files'\nodejs\node.exe |
But that makes |
It is required for (a) command names / paths that are quoted as a whole and/or (b) contain variable references or subexpressions. I've inserted "(as a whole)" in the initial post to clarify. |
In short:
|
Related: #2361, PowerShell/PowerShell#13068 (comment), and #6239
Summary of the new document or enhancement
Many special considerations apply when you call an external command-line executable (aka native application / utility), which aren't currently covered comprehensively, in one place:
That the only data type supported is text (
[string]
), both on input and output, and how raw byte data is fundamentally unsupported - both when collecting the output in PowerShell and when piping between native programs.How there are syntax pitfalls due to PowerShell's extended set of metacharacters (compared to other shells) causing potential misinterpretation of arguments, which must be avoided with quoting (e.g., To pass literal
@foo
, which works unquoted incmd.exe
andbash
, you must use'@foo'
in PowerShell).--%
can be used (primarily on Windows) to selectively deactivate PowerShell's parsing.How "native globbing" is automatically applied to arguments such as
*.txt
on Unix-like platforms; that is,*.txt
is implicitly replaced with the array of file names / paths matching that wildcard pattern.How output data is sent through the pipeline line by line, resulting in an array of strings (lines), if collected in a variable.
How stderr (standard error) output is passed through to the host rather than going through PowerShell's error stream and can only be captured with a
2>
redirection.How redirections (
>
) generally do not pass the native program's output through as-is, but invariably treat it as[Console]::OutputEncoding
encoded text that on writing to the target file is written with PowerShell's default encoding (BOM-less UTF-8 in PowerShell 6+, UTF-16LE in Windows PowerShell).How external-program calls aren't integrated with PowerShell's error handling and require explicit checking of
$?
/$LASTEXITCODE
to detect failure, except in PowerShell 7, where pipeline chain operators&&
and||
can now be used. See also: the RFC that proposes improvements to the integration.How
&
, the call operator, must be used to invoke executables whose paths are / must be quoted (as a whole) and/or contain variable references or subexpressions (this requirement isn't specific to external programs, but most likely to surface in that context).How
Start-Process
is typically not the right tool for invoking external programs - see Provide guidance as to when Start-Process is appropriate vs. direct / &-based invocation #6239.Details of requested document:
The text was updated successfully, but these errors were encountered: