Search code examples
powershellexecution

How to open a file in powershell, by entering the variable which contains the file's path?


In powershell, like in cmd, you have to type the path to the file, including the file itself , or if you are in the same location as the one where the file is located, you have to type just the files name, below is shown an example for each case :

# if the file is a different location
[Path to the file]\[fileName]

# if the file is in the same location
.\[fileName]

What I want to do is to assign the path to the file to a variable, and just type the variable name, when I want to open the file, like this :

$path = [fileLocation + fileName]

But, the problem is, that when I type the variable, PowerShell, returns me the string value of the $path , and so, how can I launch a file by having it's path assigned to a variable?


Solution

  • Just add the & sign before typing the variable which contains the path to the file like this: & $path