MicrosoftDocs / PowerShell-Docs

The official PowerShell documentation sources
https://learn.microsoft.com/powershell
Other
1.99k stars 1.57k forks source link

Introduce a conceptual help topic about calling external programs (native applications) #5152

Open mklement0 opened 4 years ago

mklement0 commented 4 years ago

Related: #2361, https://github.com/PowerShell/PowerShell/issues/13068#issuecomment-653526374, 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:

Details of requested document:

yecril71pl commented 1 year ago

C:\'Program Files\Acme Inc.\Your Terrific App.exe' is a perfect invocation and it does not use the & operator.

mklement0 commented 1 year ago

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
yecril71pl commented 1 year ago

But that makes & recommended, not required as you have suggested.

mklement0 commented 1 year ago

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.

mklement0 commented 1 year ago

In short: