如何执行从powershell添加到PATH的可执行文件?

sts*_*tis 0 powershell

我在从 Windows Powershell 执行 filename.exe(名称不相关)时遇到问题。filename.exe 可以轻松地从 cmd 执行,因为该文件的路径已添加到 PATH 环境变量中。所以我可以从命令行执行 filename.exe:

filename arg1 arg2
Run Code Online (Sandbox Code Playgroud)

我不知道如何在 Powershell 中执行此操作。

编辑: 我也尝试过同样的方法,但没有成功。我收到以下消息

    PS D:\> filename
filename : The term 'filename' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ filename
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (filename:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

Adi*_*tan 5

如果您想为该会话添加环境变量(在本例中更新 PATH),您可以使用 System.Environment 的 SetEnvironmentVariable 方法。

假设我想运行 7zip,但其可执行文件的路径不在我的路径中。所以如果我在 Powershell 中运行它,我会得到一个错误:

PS > 7z.exe
7z.exe : The term '7z.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ 7z.exe
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (7z.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
PS>
Run Code Online (Sandbox Code Playgroud)

让我们将其添加到路径中:

PS> [System.Environment]::SetEnvironmentVariable('PATH',$Env:PATH+';c:\program
files\7-zip')
Run Code Online (Sandbox Code Playgroud)

现在再次运行它:

PS > 7z.exe

7-Zip [64] 15.09 beta : Copyright (c) 1999-2015 Igor Pavlov : 2015-10-16
Run Code Online (Sandbox Code Playgroud)