0

I want to store an absolute file path in a power shell variable and use it to execute a program with arguments. I want to do this on a build server so I can have a utility on that build server used by build agents. Consider this:

$pathToExe = "C:\path\to\program.exe"

#build server does stuff in a working dir

$pathToExe arg1 arg2

When this runs, $pathToExe does not expand to its value. Instead, I see in the logs: "Unexpected token 'arg1' in expression or statement."

What's the proper way to do this kind of thing in power shell? I've tried other syntax with quotes but I can't get it to work without hardcoding the file path, which is what I'm trying to avoid.

3
  • I think you will need to use Start-Process Commented Oct 10, 2018 at 13:17
  • How so? Like this? Start-Process $pathToExe arg1 arg2 Commented Oct 10, 2018 at 13:21
  • Here is an example of one I use currently. I hardcoded my arguments but variables can be used. Start-Process -FilePath ($Firefox + "Firefox_62.exe") -ArgumentList ("-ms /INI=$Firefox" + "Firefox_Settings.ini") -Verbose -Wait -NoNewWindow Commented Oct 10, 2018 at 13:24

1 Answer 1

4

You should use call operator &. See examples below:

$executable = 'notepad.exe'
$argument = 'E:\Temp\Test.xml'

& $executable $argument
& $executable 'E:\Temp\Test.xml'

See Get-Help about_Operators or this MSDN link.

Sign up to request clarification or add additional context in comments.

3 Comments

I did not know about this method. I like it. ss64.com/ps/call.html for further information. Thanks @Pawel Dyl
The call operator is also called the invocation operator.
Thank you! This worked perfectly when I had $executable = "C:\path\program.exe".

Your Answer

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.