Is there a one-liner that will zip/unzip files (*.zip) in PowerShell?
11 Answers
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())
- 497
- 3,578
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)
- 736
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.
- 2,327
- 9,194
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':
- 361
You may wish to check out The PowerShell Community Extensions (PSCX) which has cmdlets specifically for this.
- 5,374
- 1,810
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)
- 1,720
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.
- 860
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())
}
- 121
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.
- 538