0

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

It looks to me as though you typed $prog where you meant $prog_home at one point.
@WalterMitty You are right. Fortunately sodawillow fixed it for me :-)
@MartinBrandl and alo sodawillow, great! After trying your code, I understood the working principle of Powershell a lot better now :) Thanks a lot!
You are welcome and thanks for the accept. If you wan't to persist such variables in your profile (so when you start powershell you already have $prog set, take a look at PowerShell profiles.

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.