Is there a one-liner that will zip/unzip files (*.zip) in PowerShell?
11 Answers
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.
-
+1 Linked article has useful Tasks unlike the most upvoted answer Apr 10, 2012 at 10:09
-
7-1 for suggesting a Google search. This is the top StackExchange result in a Google search for "unzip powershell" Aug 20, 2015 at 9:59
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())
-
26use $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.– PeterApr 19, 2011 at 4:06
-
3The line
$destination.Copyhere($zip_file.items())
does the actual unziping. Oct 28, 2011 at 22:02 -
2You 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()) }
Jan 29, 2013 at 11:19 -
6
-
2This fails for me when the zip file contains just a folder (items is empty) Aug 19, 2014 at 9:16
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)
-
1This would be great if there were a simple method to ExtractToDirectory and an option to overwrite all existing files. Apr 2, 2013 at 0:43
-
1
-
3Load the assembly with Add-Type, for example: Add-Type -AssemblyName System.IO.Compression.FileSystem– MikeSep 19, 2013 at 2: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. ;)– MikeSep 19, 2013 at 2:44
-
3
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':
-
5
You may wish to check out The PowerShell Community Extensions (PSCX) which has cmdlets specifically for this.
-
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_sJun 12, 2009 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 Apr 27, 2011 at 15:02
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.
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)
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())
}
The ionic approach rocks:
https://dotnetzip.codeplex.com/wikipage?title=PS-Examples
supports passwords, other crypto methods, etc.
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.