How do I do the bash equivalent of $PROGPATH/program in Powershell? How do I do the bash equivalent of $PROGPATH/program in Powershell? powershell powershell

How do I do the bash equivalent of $PROGPATH/program in Powershell?


js2010's helpful answer shows the correct solution:

Because your command name/path contains a variable reference ($PROGPATH/...), you must invoke it with &.
The same applies if a grouping expression, (...) is used, or a subexpression, $(...) is involved.

Additionally, the same applies if a command name/path is quoted ('...' or "...")[1], as is required if the path contains spaces, for instance.

To put it differently: Direct invocation is only supported if the command name/path is a verbatim, unquoted string[1]; in all other cases, & must be used.

As for why:

&, the call operator is necessary to force interpretation of a statement as a command, i.e. to have it parsed in argument mode (see below), so as to result in command execution rather than expression evaluation.

PowerShell has two fundamental parsing modes:

  • argument mode, which works like a traditional shell, where the first token is a command name/path, such as a cmdlet or an external program, with subsequent tokens representing the arguments, which only require quoting if they contain shell metacharacters (chars. with special meaning to PowerShell, such as spaces to separate tokens).

  • expression mode, which works like expressions in programming languages.

PowerShell decides based on a statement's first token what parsing mode to apply:

If, among other things, the first token starts with a variable reference or is a quoted string, PowerShell parses in expression mode.

  • In expression mode, \ starts a new token, and unrecognized token \program results in the syntax error you saw.
  • (If you had used /, it would have been interpreted as the division operator, and program wouldn't be a valid divisor operand.)

[1] Note that if your executable path is a literal string (doesn't contain variable references of expressions) you may alternatively `-escape individual characters (spaces) in lieu of enclosing entire string in '...' or "...", in which case & is then not necessary; e.g.:
C:\Program` Files\Notepad++\notepad++.exe
With a literal string you can even employ partial single- or double-quoting as long as the first token is unquoted; e.g.:
C:\"Program Files"\Notepad++\notepad++.exe


Use the call operator "&". https://ss64.com/ps/call.html

Related: Executing a command stored in a variable from PowerShell

$progpath = 'c:\windows\system32'& $progpath\notepad somefile.txt

Something with a space:

 & 'C:\Program Files\internet explorer\iexplore' yahoo.com

Other options, adding to the path:

$env:path += ';C:\Program Files\internet explorer'iexplore yahoo.com

And backquoting the spaces:

C:\Program` Files\internet` explorer\iexplore yahoo.com