5

In GNU/Linux I would do:

PROGPATH=/long/and/complicated/path/to/some/bin
$PROGPATH/program args...

but in Powershell if I try this:

$PROGPATH=\long\and\complicated\path\to\some\bin
$PROGPATH\program args...

I get:

At script.ps1:2 char:...
+ $PROGPATH\program args ...
+ ~~~~~~~~
Unexpected token '\program' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken

So how do I do this simple thing I know how to do in bash, in Powershell?

mklement0
452k68 gold badges726 silver badges986 bronze badges
asked Aug 27, 2019 at 14:43
1
  • 1
    take a look at the call operator, and at Start-Process. Commented Aug 27, 2019 at 14:55

2 Answers 2

5

js2010's helpful answer shows the correct solution:

Because your command name/path contains a variable reference ($PROGPATH/...), you must invoke it with &:
& $PROGPATH\program args...
The same applies if a grouping expression, (...) 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, as a result, the 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

answered Aug 27, 2019 at 15:34
Sign up to request clarification or add additional context in comments.

Comments

3

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
answered Aug 27, 2019 at 15:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.