Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
related: Unzip with PS in Server Core –  Ruben Bartelink Aug 28 '13 at 16:20
add comment

8 Answers

up vote 18 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.

share|improve this answer
    
+1 Linked article has useful Tasks unlike the most upvoted answer –  Ruben Bartelink Apr 10 '12 at 10:09
add comment

James Holwell I like your answer but I expanded it a little bit

# Example
#unzip "myZip.zip" "C:\Users\me\Desktop" "c:\mylocation"
function unzip($fileName, $sourcePath, $destinationPath)
{
    $shell = new-object -com shell.application
    if (!(Test-Path "$sourcePath\$fileName"))
    {
        throw "$sourcePath\$fileName does not exist" 
    }
    New-Item -ItemType Directory -Force -Path $destinationPath -WarningAction SilentlyContinue
    $shell.namespace($destinationPath).copyhere($shell.namespace("$sourcePath\$fileName").items()) 
}
share|improve this answer
add comment

Now in .NET Framework 4.5, there is a ZipFile class that you can use like this:

[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
[System.IO.Compression.ZipFile]::ExtractToDirectory($sourceFile, $targetFolder)
share|improve this answer
    
Seems to be in 4.0 as well - at least, I don't have 4.5 installed and this still worked. –  GalacticCowboy Feb 13 '13 at 1:07
    
@GalacticCowboy: hmm, not what msdn says about this class… Glad it helped, and thank you for sharing, but please check again :] –  mousio Feb 13 '13 at 1:11
3  
Ok, withdrawn. :) Apparently 4.5 just takes over 4.0 instead of a separate install like 3.5 was. You have to look in the registry to find out, but it actually is 4.5... –  GalacticCowboy Feb 13 '13 at 1:18
1  
Load the assembly with Add-Type, for example: Add-Type -AssemblyName System.IO.Compression.FileSystem –  Mike Sep 19 '13 at 2:39
1  
@JamesDunne - If you don't have other files you need to preserve, could use 'Remove-Item -Recurse $TargetFolder'. Otherwise, what you want can be done, but it would be non-trivial. You would need to open the zip for read, and then walk the zip, deleting any previous target object and unpacking the new one. Lucky for me, the easy solution works. ;) –  Mike Sep 19 '13 at 2:44
show 4 more comments

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())
share|improve this answer
23  
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? –  D3vtr0n Oct 18 '11 at 22:16
3  
The line $destination.Copyhere($zip_file.items()) does the actual unziping. –  Jonathan Allen Oct 28 '11 at 22:02
1  
You can parcel the above into a function, if you wanted: function unzip($filename) { if (!(test-path $filename)) { throw "$filename does not exist" } $shell = new-object -com shell.application $shell.namespace($pwd.path).copyhere($shell.namespace((join-path $pwd $filename)).items()) } –  James Holwell Jan 29 '13 at 11:19
3  
This should be the accepted answer. –  tugberk Apr 19 '13 at 21:06
show 1 more comment

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)
share|improve this answer
add comment

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

share|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
add comment

WinRAR can work in CMD mode accepting arguments

share|improve this answer
add comment

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/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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