如何通过输入包含文件路径的变量在powershell中打开文件?

Eug*_*ban 3 powershell execution

powershell中,与在 中一样cmd,您必须键入文件的路径,包括文件本身,或者如果您与文件所在的位置相同,则只需键入文件名,下面显示了每种情况的示例:

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

# if the file is in the same location
.\[fileName]
Run Code Online (Sandbox Code Playgroud)

我想要做的是将文件的路径分配给一个变量,当我想打开文件时只需键入变量名称,如下所示:

$path = [fileLocation + fileName]
Run Code Online (Sandbox Code Playgroud)

但是,问题是,当我键入变量时,PowerShell 会返回 的字符串值$path,因此,如何通过将文件的路径分配给变量来启动文件?

小智 5

&只需在键入包含文件路径的变量之前添加符号,如下所示:& $path