0

I'm trying to pass named arguments to a batch file referred to using environment variables in Windows. The environment variable is a direct path to the .bat file, and can easily be executed in Powershell using $env:MY_VAR which promptly prints the full path to the batch. However if I attempt to pass arguments Powershell will complain about unexpected tokens. What I tried was:

$env:MY_VAR -data some_data.txt -other more_data.txt

$env:MY_VAR --data some_data.txt --other more_data.txt

$env:MY_VAR --data=some_data.txt --other=more_data.txt

$env:MY_VAR -data=some_data.txt -other=more_data.txt

and many more ... I also tried encapsulating my values in quotes in case it was something to do with their naming. None of these worked, but if I were to use the direct path as such:

FULL_PATH/program.bat -data some_data.txt -other more_data.txt

it will work just fine. Does Powershell interpret environment variables in some particular way that requires argument passing to be done differently? My Google searches does not yield any useful results, and it is mostly people trying to start Powershell from within a batch script... Not exactly what I'm looking for. I've also come across extensions for Powershell, but that just seemed like an odd solution.

Any help is welcome and appreciated.

2 Answers 2

3

You need to use the & invocation operator to tell powershell that the string you are passing is a command to invoke:

& $env:MY_VAR -data some_data.txt -other more_data.txt

For more details see this page.

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

1 Comment

This worked perfectly! I simply was not aware of that, but it makes sense. Thanks to the both of you.
1

Environment variables are just a string. If you want to execute what's in your variable you first need to tell PowerShell that's what you want. In order to do that, you can use &.

& $env:MY_VAR some_data.txt more_data.txt

Comments

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.