PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 19, 2022

[FIXED] How to pass arguments when executing environment variable in Powershell?

 August 19, 2022     batch-file, command-prompt, environment-variables, powershell, windows     No comments   

Issue

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.


Solution

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.



Answered By - zdan
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing