2

Dears,

I have an environment variable containing all the arguments to pass to a program. VAR_NAME=/arg1 /arg2 /arg3 ...

I tried invoking the program using:

program $Env.VAR_NAME

Unfortunately the environment variable expends to a single argument.

Thanks.

Baylej
  • 43
  • 6

1 Answers1

2

Just split the environment variable to return a list of arguments:

program ($Env.VAR_NAME -split ' ')
Martin Brandl
  • 51,813
  • 12
  • 118
  • 151
  • 1
    Nicely done; just to clarify the constraints: `program` is assumed to be an _external_ program rather than a PowerShell command, and simple splitting by spaces won't work if the value contains quoted arguments that have embedded spaces themselves - [this answer](https://stackoverflow.com/a/69170814/45375) to a related question tries to cover all angles. As an aside: `program (-split $Env.VAR_NAME)` provides more flexibility, because it also tolerates multiple spaces between embedded arguments and ignore leading and trailing whitespace. – mklement0 Sep 14 '21 at 02:02