104

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

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
BlueGene
  • 2,191
  • 9
  • 29
  • 33
  • related: [Unzip with PS in Server Core](http://serverfault.com/questions/446884/unzip-file-with-powershell-in-server-2012-core) – Ruben Bartelink Aug 28 '13 at 16:20

11 Answers11

154

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())
ulidtko
  • 395
  • 3
  • 12
Ameer Deen
  • 3,598
  • 4
  • 25
  • 27
56

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)
mousio
  • 736
  • 5
  • 9
  • It *definitely* worked - I used it to extract over 400 zip files. I'll check to make sure I don't have 4.5 on here, but it doesn't show up under Microsoft.NET. – GalacticCowboy Feb 13 '13 at 01:13
  • 1
    This *would* be great if there were a simple method to ExtractToDirectory and an option to overwrite all existing files. – James Dunne Apr 02 '13 at 00:43
  • 1
    This works with .NET 4.5. You do however need Powershell V3 too. – DalSoft Jun 16 '13 at 19:29
  • [Article with example](http://vcsjones.com/2012/11/11/unzipping-files-with-powershell-in-server-core-the-new-way/) based on [related question](http://serverfault.com/questions/446884/unzip-file-with-powershell-in-server-2012-core) – Ruben Bartelink Aug 28 '13 at 16:17
  • 3
    Load the assembly with Add-Type, for example: Add-Type -AssemblyName System.IO.Compression.FileSystem – Mike Sep 19 '13 at 02:39
  • 2
    @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 02:44
  • 3
    Be warned that relative file locations will use the *.NET* current directory, *not* the PowerShell one. See [here](http://stackoverflow.com/q/11246068/1394393). It's probably better just to `(Resolve-Path $someDir).Path` the arguments. – jpmc26 Aug 13 '16 at 03:45
  • Oh, I know this is 6 years old, but after trying to find a low-cost commercial (or open source) solution to extract files from a PKZIP SFX, and failing miserably, this solution right here made my day! – Shoeless Aug 30 '18 at 17:28
21

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.

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
Kevin Kuphal
  • 9,064
  • 1
  • 34
  • 41
19

I know this is a very old question, but I just saw it linked on Twitter on figured I'd post a current answer.

PowerShell 5, currently available on Windows 10 or via the Windows Management Framework 5 Production Preview, comes with two built-in cmdlets for 'zipping' and 'unzipping':

Windos
  • 361
  • 3
  • 7
18

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

Richard
  • 5,309
  • 1
  • 22
  • 20
Kevin Colby
  • 1,760
  • 10
  • 14
  • 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 07: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
5

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)
Nathan Hartley
  • 1,620
  • 5
  • 26
  • 38
5

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/

Brian Reiter
  • 860
  • 5
  • 8
2

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()) 
}
Nico
  • 121
  • 3
0

WinRAR can work in CMD mode accepting arguments

adopilot
  • 1,501
  • 6
  • 25
  • 40
0

The ionic approach rocks:

https://dotnetzip.codeplex.com/wikipage?title=PS-Examples

supports passwords, other crypto methods, etc.

0

I've created a PowerShell 2.0 compatible module that uses the native Windows OS commands to zip and unzip files in a synchronous manner. This works on older OSs, like Windows XP, and does not require .Net 4.5 or any other external tools. The functions will also block script execution until the files have all been zipped/unzipped. You can find more information and the module on my blog here.

deadlydog
  • 303
  • 1
  • 5
  • 9