How to zip/unzip files in Powershell?

137,446

Solution 1

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.

Solution 2

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())

Solution 3

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)

Solution 4

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':

Solution 5

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

Share:
137,446

Related videos on Youtube

BlueGene
Author by

BlueGene

Updated on September 17, 2022

Comments

  • BlueGene
    BlueGene over 1 year

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

  • Peter
    Peter about 13 years
    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.
  • Steve
    Steve about 13 years
    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
  • Jan Vojtěch Vaníček
    Jan Vojtěch Vaníček over 12 years
    Ok but how do you unzip?
  • Jonathan Allen
    Jonathan Allen over 12 years
    The line $destination.Copyhere($zip_file.items()) does the actual unziping.
  • Adam M-W
    Adam M-W about 12 years
    +1 Linked article has useful Tasks unlike the most upvoted answer
  • GalacticCowboy
    GalacticCowboy over 11 years
    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.
  • James Dunne
    James Dunne about 11 years
    This would be great if there were a simple method to ExtractToDirectory and an option to overwrite all existing files.
  • tugberk
    tugberk about 11 years
    This should be the accepted answer.
  • DalSoft
    DalSoft almost 11 years
    This works with .NET 4.5. You do however need Powershell V3 too.
  • Adam M-W
    Adam M-W over 10 years
  • Mike
    Mike over 10 years
    @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. ;)
  • m3nda
    m3nda over 10 years
    Agree about accepted. Is not a monstruous code like other PowerShell i see. 5 Lines is just great and is fully related to Powershell not to 3rd party software.
  • James Woolfenden
    James Woolfenden almost 10 years
    This fails for me when the zip file contains just a folder (items is empty)
  • Kiquenet
    Kiquenet over 9 years
    @Nico answer working for unzip in a destination
  • craibuc
    craibuc almost 9 years
    Does Copyhere support passwords?
  • machine yearning
    machine yearning almost 9 years
    -1 for suggesting a Google search. This is the top StackExchange result in a Google search for "unzip powershell"
  • ujeenator
    ujeenator over 8 years
    Great! This works on PowerShell v2.0 and Windows 7
  • Jerome2606
    Jerome2606 almost 8 years
    How to zip using your way ?
  • jpmc26
    jpmc26 almost 8 years
    It is a good thing to provide new information to old questions. ;) +1
  • jpmc26
    jpmc26 almost 8 years
    Be warned that relative file locations will use the .NET current directory, not the PowerShell one. See here. It's probably better just to (Resolve-Path $someDir).Path the arguments.
  • johny why
    johny why over 7 years
    yes, how to zip with CopyHere?
  • johny why
    johny why over 7 years
    how to zip with this method (> is command-prompt, src is a folder): >$shellapp=new-object -com shell.application >$zippath="test.zip" >$zipobj=$shellapp.namespace((Get-Location).Path + "\$zippath") >$srcpath="src" >$srcobj=$shellapp.namespace((Get-Location).Path + "\$srcpath") >$zipobj.Copyhere($srcobj.items())
  • Shoeless
    Shoeless over 5 years
    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!