我在Windows Powershell中执行filename.exe(名称不相关)时遇到问题。 filename.exe可以很容易地从cmd执行,因为文件的路径被添加到PATH环境变量中。 所以我可以从命令行执行这样的filename.exe:
filename arg1 arg2
我不知道如何在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
答案 0 :(得分:1)
如果您想为该会话添加环境变量,在本例中更新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>
让我们将其添加到路径中:
PS> [System.Environment]::SetEnvironmentVariable('PATH',$Env:PATH+';c:\program
files\7-zip')
现在再次运行:
PS > 7z.exe
7-Zip [64] 15.09 beta : Copyright (c) 1999-2015 Igor Pavlov : 2015-10-16