Running 7-Zip from within a Powershell script
Asked Answered
K

8

52

I'm trying to use 7-Zip to backup some files inside a Powershell (v2) script.

I have:

$zipPath = "C:\Program Files\7-Zip\7z.exe"
[Array]$zipArgs = "-mx=9 a", "`"c:\BackupFolder\backup.zip`"", "`"c:\BackupFrom\backMeUp.txt`""

&$zipPath $zipArgs;

But when I run this I get:

7-Zip [64] 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18


Error:
Incorrect command line

Writing this to the screen I get:

C:\Program Files\7-Zip\7z.exe -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"

So I assumed that I needed to put quotes around the path to 7z.exe, that gave me:

$zipPath = "C:\Program Files\7-Zip\7z.exe"
$zipPath = " `"$zipPath`" "
[Array]$zipArgs = "-mx=9 a", "`"c:\BackupFolder\backup.zip`"", "`"c:\BackupFrom\backMeUp.txt`""

&$zipPath $zipArgs;     

But then I get the following error:

    The term '"C:\Program Files\7-Zip\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 c
orrect and try again.
At C:\BackupScript\Backup.ps1:45 char:22
+                     & <<<< `"$zipPath`" $zipArgs;                    
    + CategoryInfo          : ObjectNotFound: ("C:\Program Files\7-Zip\7z.exe":String) [], CommandNotFound 
   Exception
    + FullyQualifiedErrorId : CommandNotFoundException

Writing it out gives me:

"C:\Program Files\7-Zip\7z.exe" -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"

Which works as expected when pasting straight into a command window. I have been trying to figure this out for a while, but assume I am missing something (probably quite obvious). Can anybody see what I need to do to make this run?

Ketty answered 13/8, 2014 at 13:50 Comment(4)
An article I wrote recently may be helpful: Running Executables in PowerShell. Try & "C:\Program Files\7-Zip\7z.exe" "-mx=9" a C:\BackupFolder\Backup.zip C:\BackupFrom\backMeUp.txt.Ruthie
Cheers, that's an interesting article. I'll need to have a proper read later. I think that you may have the answer. Whereas I had "-mx=9 a" what I needed was "-mx=9" a. I can't make it work with $zipArgs variable, but it does work with & "$zipPath" "-mx=9" a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"Ketty
Correct; to prevent PowerShell from interpreting -mx=9 as an operator, use quotes or escape the parameter with a backtick (as explained in the article).Ruthie
Try &$zipPath @zipArgsIllinois
T
87

Found this script and adapted it to your needs. Can you please try:

$7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"

if (-not (Test-Path -Path $7zipPath -PathType Leaf)) {
    throw "7 zip executable '$7zipPath' not found"
}

Set-Alias Start-SevenZip $7zipPath

$Source = "c:\BackupFrom\backMeUp.txt"
$Target = "c:\BackupTo\backup.zip"

Start-SevenZip a -mx=9 $Target $Source
Timmerman answered 13/8, 2014 at 14:24 Comment(4)
Cheers for that. I got that working. I'll need to spend some time with that syntax is I'm not familiar with it (it's always good to learn new ways of doing things). cheersKetty
Nice cheeky bit of alias work there which really helps make calls to 7z much easier. Thanks!Cockspur
While I am not suggesting that aliases are always a good thing, one could also call it 7z. set-alias 7z "$env:ProgramFiles\7-Zip\7z.exe"Protecting
Docs at sevenzip.osdn.jp/chm/cmdline/commands/add.htm. They helped me figured out that I needed to cd into the directory to avoid parent folders being copied to the zip.Quito
B
16

put "&" special character before 7z command. Example: &7z ...

Brier answered 6/10, 2015 at 10:3 Comment(0)
N
15

Simply prefix the command with an ampersand

& "C:\Program Files\7-Zip\7z.exe" -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"
Naamann answered 3/1, 2018 at 17:11 Comment(0)
I
3

Maybe a simpler solution is to run 7-zip on your Powershell via cmd:

cmd /c 7za ...
Iberia answered 16/1, 2015 at 7:11 Comment(1)
I might try it! I notice ps's $LASTEXITCODE [1] should get the nice return code [2] too. ; Refs. 1.[] ; X.Returning an error code from CMD to Powershell - Stack Overflow ;; #11005166 ; 2.[] ; X.Exit Codes from 7-Zip ;; sevenzip.sourceforge.jp/chm/cmdline/exit_codes.htmTractile
D
2

If you adapt it correctly: Dont forget the "" on "$Target" and avoid $7zipPath in c:\programm files with a space in the path

Set-Alias 7zip $7zipPath

$Source = "c:\BackupFrom\backMeUp.txt"
$Target = "c:\BackupFolder\backup.zip"

7zip a -mx=9 "$Target" "$Source"

or

7z a "$ArchiveName" -t7z '@listfile.txt'
Depress answered 22/4, 2020 at 12:20 Comment(1)
Quotes didn't work for me. I had to cd into the directory to avoid the parent folders being dumped into the archive sevenzip.osdn.jp/chm/cmdline/commands/add.htmQuito
V
1
C:\'Program files'\7-Zip\7z.exe a '$archiveFile'  -Path '$dest'

where:

  • archiveFile = name of the archive file name.
  • dest = destination folder.
Vaenfila answered 25/1, 2022 at 22:4 Comment(1)
That single quotation marks comes in clutchMatsu
E
0

try to use parameter -file to specify the location of program or script:

-file "C:\Program Files\someting.exe"

Electrostatic answered 27/1, 2017 at 8:38 Comment(0)
I
0

The reason for the error is because each command line option needs to be a separate element in the array, so

[Array]$zipArgs = "-mx=9", "a", "c:\BackupFolder\backup.zip", "c:\BackupFrom\backMeUp.txt"

instead of

[Array]$zipArgs = "-mx=9 a", "c:\BackupFolder\backup.zip", "c:\BackupFrom\backMeUp.txt"

Note, that the "a" needs to be a separate element. Also the filenames do not need embedded quotes in them (they might if they included spaces, I'm not sure).

Iberian answered 23/2 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.