Is there a one-liner that will zip/unzip files (*.zip) in PowerShell?

link|improve this question

feedback

6 Answers

up vote 9 down vote accepted

DotNetZip will allow you to do this from PowerShell. It is not a one-liner, but the library will allow you to write the PowerShell script you need.

You can also use the COM interface, see Compress Files with Windows PowerShell then package a Windows Vista Sidebar Gadget.

Googling "zip powershell" or "unzip powershell" might also turn up useful results.

link|improve this answer
+1 Linked article has useful Tasks unlike the most upvoted answer – Ruben Bartelink Apr 10 at 10:09
feedback

This is how you can do it purely from Powershell without any external tools. This unzips a file called test.zip onto the current working directory:

$shell_app=new-object -com shell.application
$filename = "test.zip"
$zip_file = $shell_app.namespace((Get-Location).Path + "\$filename")
$destination = $shell_app.namespace((Get-Location).Path)
$destination.Copyhere($zip_file.items())

link|improve this answer
10  
use $destination.Copyhere($zip_file.items(), 0x10) for overwriting existing files. 0x4 hides the dialog box, and 0x14 combines these and overwrites and hides the dialog. – Peter Apr 19 '11 at 4:06
Ok but how do you unzip? – Devtron Oct 18 '11 at 22:16
1  
The line $destination.Copyhere($zip_file.items()) does the actual unziping. – Jonathan Allen Oct 28 '11 at 22:02
feedback

You may wish to check out The PowerShell Community Extensions (PSCX) which has cmdlets specifically for this.

link|improve this answer
1  
+1 PSCX is a great set of add-on cmdlets - I just wish I could pick and choose more which ones I want and which I don't. It seems to be changing a whole lot in my Powershell instance.... – marc_s Jun 12 '09 at 7:48
I've come across this because I actually want to automate the PSCX installation if I can for some coworkers. Trying it now to see what sort of issues I run into – jcolebrand Apr 27 '11 at 15:02
feedback

I find the simplest solution to just use infozip binaries which I have used for years and use in a UNIX environment.

PS> zip -9r ../test.zip * 
PS> cd .. 
PS> unzip -t test.zip Archive:  test.zip
    testing: LinqRepository/          OK
    testing: LinqRepository/ApplicationService.cs   OK
    testing: LinqRepository/bin/      OK 
... 
No errors detected in compressed data of test.zip.

It would be straighforward to put a powershell wrapper around the text output but in practice I never need that so I haven't bothered.

http://www.info-zip.org/

link|improve this answer
feedback

I also like Info-ZIP (the Zip engine found in most other Zip utilities) and 7-Zip, another favorite which has both a GUI and command line Zip utility. The point being, there are some good command-line utilities that will work for most PowerShell tasks.

There are some tricks to running command line utilities that were not built with PowerShell in mind:

  • Running an executable that starts with a number in the name, preface it with an Ampersand (&).

    &7zip.exe

  • Wrap each token, the utility is expecting to see from the command line, in quotes.

    &"c:\path with space\SomeCommand.exe" "/parameter2" "/parameter2" "parameter2's Value" "Value2 `" with a quote"

Try this:

zip filename.zip (Get-ChildItem somepath\*)

Or even:

zip filename.zip (Get-Content ListOfFiles.txt)
link|improve this answer
feedback

WinRAR can work in CMD mode accepting arguments

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.