3

Why cannot I run 7z in the following PowerShell script?

Set-Location "C:\Program Files\7-Zip"
Invoke-Expression "dir"
Invoke-Expression "7z"
Invoke-Expression "7z.exe"

Result:

Directory: C:\Program Files\7-Zip


Mode                LastWriteTime     Length Name                                                                                                                        
----                -------------     ------ ----                                                                                                                        
-a---        11/18/2010   9:08 PM      91020 7-zip.chm                                                                                                                   
-a---        11/18/2010   9:08 PM      86016 7-zip.dll                                                                                                                   
-a---        11/18/2010   9:24 PM    1422336 7z.dll                                                                                                                      
-a---        11/18/2010   9:08 PM     284160 7z.exe                                                                                                                      
-a---        11/18/2010   9:27 PM     162816 7z.sfx                                                                                                                      
-a---        11/18/2010   9:27 PM     152064 7zCon.sfx                                                                                                                   
-a---        11/18/2010   9:10 PM     740352 7zFM.exe                                                                                                                    
-a---        11/18/2010   9:11 PM     387072 7zG.exe                                                                                                                     
-a---         9/10/2010  11:41 AM        333 descript.ion                                                                                                                
-a---        11/18/2010   9:11 PM      32400 History.txt                                                                                                                 
-a---          1/2/2010   3:18 PM       1927 License.txt                                                                                                                 
-a---        11/18/2010   9:12 PM       1565 readme.txt                                                                                                                  
7z : The term '7z' 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
+ ~~
    + CategoryInfo          : ObjectNotFound: (7z:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

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
5
  • 1
    7z is a hassle to deal with in powershell. You need to use the & syntax. It's the only way to make it work. Commented May 11, 2016 at 2:54
  • Check out these uses of & and Start and see if they help solve the problem Commented May 11, 2016 at 2:56
  • I tried Invoke-Expression "& 7z", but it still gave me an error. Did I use the & correctly? Commented May 11, 2016 at 3:03
  • 1
    Because when you are in a folder in PowerShell, you cannot run things in that folder directly unless you specify .\ first, e.g. .\7z.exe. Commented May 11, 2016 at 3:05
  • Thank you. Invoke-Expression "./7z" worked. .\7z also worked. Commented May 11, 2016 at 3:06

1 Answer 1

4

By default, Powershell does look for executables on PATH, but not in current directory (.\). You could:

  1. Preppend ".\" to the name of executable when calling from current directory:

    Invoke-Expression ".\7z"
    
  2. Call it by full path (using mentioned & syntax):

    & "c:\Program Files\7-zip\7z"
    
  3. add executable's directory to PATH ($env:Path in Powershell):

    $env:Path += "c:\Program Files\7-zip"
    Invoke-Expression "7z"
    
Sign up to request clarification or add additional context in comments.

2 Comments

If I were to use the third method, is it temporary (only affects that script while the script is running) or is it permanent (the same effect as adding the path in the System Variables dialogue)?
It is temporary - it modifies PATH variable just for the current process. If you want to make it permanent, use [Environment]::SetEnvironmentVariable (technet.microsoft.com/en-us/library/ff730964.aspx).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.