简体   繁体   中英

How to execute executable which added to PATH from powershell?

I'm having problems execute the filename.exe (the name is not relevant) from Windows Powershell. The filename.exe can be executed from the cmd easily, because the file's path is added to the PATH environment variable. So I can execute the filename.exe like this from command line:

filename arg1 arg2

I have no idea how to do so in the Powershell.

Edit: I've tried the same way but no success. I got the following message

    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

If you would like to add an environment variable for that session, in this case update PATH, you can use System.Environment's SetEnvironmentVariable method.

Say, I want to run 7zip, but the path to its executable is not in my path. So if I run it in Powershell, I will get an error:

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>

Let's add it to the path:

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

And now run it again:

PS > 7z.exe

7-Zip [64] 15.09 beta : Copyright (c) 1999-2015 Igor Pavlov : 2015-10-16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM