How to zip/unzip files in Powershell?
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.
Related videos on Youtube
BlueGene
Updated on September 17, 2022Comments
-
BlueGene over 1 year
Is there a one-liner that will zip/unzip files (*.zip) in PowerShell?
-
Adam M-W over 10 yearsrelated: Unzip with PS in Server Core
-
-
Peter about 13 yearsuse $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 about 13 yearsI'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 over 12 yearsOk but how do you unzip?
-
Jonathan Allen over 12 yearsThe line
$destination.Copyhere($zip_file.items())
does the actual unziping. -
Adam M-W about 12 years+1 Linked article has useful Tasks unlike the most upvoted answer
-
GalacticCowboy over 11 yearsIt 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 about 11 yearsThis would be great if there were a simple method to ExtractToDirectory and an option to overwrite all existing files.
-
tugberk about 11 yearsThis should be the accepted answer.
-
DalSoft almost 11 yearsThis works with .NET 4.5. You do however need Powershell V3 too.
-
Adam M-W over 10 yearsArticle with example based on related question
-
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 over 10 yearsAgree 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 almost 10 yearsThis fails for me when the zip file contains just a folder (items is empty)
-
Kiquenet over 9 years@Nico answer working for unzip in a destination
-
craibuc almost 9 yearsDoes
Copyhere
support passwords? -
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 over 8 yearsGreat! This works on PowerShell v2.0 and Windows 7
-
Jerome2606 almost 8 yearsHow to zip using your way ?
-
jpmc26 almost 8 yearsIt is a good thing to provide new information to old questions. ;) +1
-
jpmc26 almost 8 yearsBe 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 over 7 yearsyes, how to zip with CopyHere?
-
johny why over 7 yearshow 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 over 5 yearsOh, 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!