Rui
Rui

Reputation: 3673

How to use a variable as part of the path of a calling program

As a new PowerShell user, I am planning to call a local program whose path is not in the PATH environment variable. When calling the program the relative or full path of the program has to be given, e.g. .\Users\xxx\Downloads\program_name\bin\prog. However, as this path is way long and sometimes might be changed, so as a Linux Shell script programmer I would like to use a variable in the path, for instance .\$prog_home\bin\prog.

Hereby my question is how to initialize this variable so that it can be used as I assumed beforehands? I tried to initialized the variable in such way -

$prog_home="User\xxx\Downloads\program_name"

Unfortunately, this really can not work at all like in Linux Shell

Upvotes: 0

Views: 186

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

Please consider to use the Join-Path cmdlet to combine a path. There is also a builtin USERPROFILE environment variable which you can use. This is how you could do it:

$prog_home = Join-Path $env:USERPROFILE 'program_name'
$prog = Join-Path $prog_home '\bin\prog.exe'
& $prog #execute it

Upvotes: 2

Related Questions