- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Zip / Unzip files or folders using PowerShell?
Now it is easy to ZIP or extract (unzip) the files or folders using PowerShell. PowerShell has added features of the Archive module (Microsoft.PowerShell.Archive) from PowerShell 5.1 version.
If you have PowerShell older version (version 4.0 or less) then you can download and install the module from the website or through the command line. However, only download and installing or manually copying the Archive module to the Module folder won’t work because it needs some dependent DLL files which are provided by .Net framework 4.5 so you need to consider upgrading .Net framework as well if it is below the 4.5 version.
Once you have the appropriate PowerShell version and .Net framework version, and if you have installed the Archive module or if it is default installed, check the commands are supported for the archive module as shown below.
PS C:\WINDOWS\system32> Get-Command -Module *Archive* |ft -AutoSize CommandType Name Version Source ----------- ---- ------- ------ Function Compress-Archive 1.0.1.0 Microsoft.PowerShell.Archive Function Expand-Archive 1.0.1.0 Microsoft.PowerShell.Archive
So there are two commands included in this module. Compress-Archive command is to compress files and folders and to Expand-Archive to extract zipped data.
Compress-Archive command to compress the files and folders.
Before starting the examples of the Compress-Archive command, we will see the cmdlet syntax first.
Compress-Archive -LiteralPath <String[]> [-DestinationPath] <String> [-CompressionLevel <String>] -Force -Update [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]
Here, the main parameters are Source files / folders, destination path, compression level, force, and update.
Compression Level
There are three types of compression level.
- Fastest − Uses the fastest compression to reduce the processing time. It can lead to a larger file size.
- Optimal − Normal compression level. Processing time depends on the size of the file. This is the default compression level if you don’t use compression parameter.
- NoCompression − Doesn’t compress the source files. This means the size of the actual folders and files are the same after the zipped only there is no compression ratio.
Force
When specified this parameter, it will overwrite the files and folders.
Update
This parameter updates the files from the source to the destination which are newer or updated instead of zipping the entire content again.
Examples
1. To compress a single folder.
Compress-Archive C:\temp\* -DestinationPath c:\temp.zip -CompressionLevel Fastest
The above command will compress the folder C:\temp the destination path C:\ with the ZIP name temp.zip and with the compression level Fastest.
If you try to zip the folders with the same destination name which already exists then it will pop an error.
PS E:\scripts\Powershell> Compress-Archive C:\temp\* -DestinationPath c:\temp.zip -CompressionLevel Fastest Compress-Archive : The archive file C:\temp.zip already exists. Use the - Update parameter to update the existing archive file or use the -Force parameter to overwrite the existing archive file. At line:1 char:1 + Compress-Archive C:\temp\* -DestinationPath c:\temp.zip -CompressionL ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (C:\temp.zip:String) [Compress- Archive], IOException + FullyQualifiedErrorId : ArchiveFileExists,Compress-Archive
To overwrite the destination file, you need to use the –Force parameter.
Compress-Archive C:\temp\* -DestinationPath c:\temp.zip -CompressionLevel Fastest -Force
If you want to update any file inside the temp folder and if you need to update the destination zip then you need to use –Update parameter. When you use Update the parameter you don’t need to use the -Force parameter because –Update parameter will also overwrite destination files along with updating the files and even if the destination file doesn’t exist, it will create a new file.
Compress-Archive C:\temp\* -DestinationPath c:\temp.zip -CompressionLevel Fastest -Update
2. Zip multiple files and folders.
Multiple Files at different locations −
$compress = @{ Path = "C:\temp\ComputerInformation.csv","D:\cars.xml" DestinationPath = "C:\archive.zip" CompressionLevel = "Fastest" } Compress-Archive @compress -Verbose
Multiple Folders at different locations −
$compress = @{ Path = "C:\temp\*","C:\Dell\*" DestinationPath = "C:\folderarchive.zip" CompressionLevel = "Fastest" } Compress-Archive @compress -Force -Verbose
Expand-Archive command.
Expand-Archive command is useful for extracting the ZIP files. This command is a simple comparatively Compress-Archive command as it needs only Source and the destination ZIP files.
In the below example, we are going to extract folderarchive.zip file to the destination location.
Expand-Archive C:\folderarchive.zip -DestinationPath C:\Unzipedfolder -Verbose
Here, the Destination folder UnzipedFolder at the destination path doesn’t exist but the cmdlet will create the folder automatically if doesn’t exist. If the folder already exists then you need to use the – Force parameter to overwrite the files and folders.
Expand-Archive C:\folderarchive.zip -DestinationPath C:\Unzipedfolder –Force -Verbose
- Related Articles
- How to display files/folders including hidden files/folders in PowerShell?
- How to retrieve files and folders attributes using PowerShell?
- How to change files and folders attributes using PowerShell?
- How to get hidden files and folders using PowerShell?
- How to delete empty files and folders using PowerShell?
- How to delete hidden files and folders using PowerShell?
- How to get only hidden files and folders in PowerShell?
- How to get only files but not the folders with Get-ChildItem using PowerShell?
- How to copy readonly and hidden files/folders in the PowerShell?
- How to copy files/folders to the remote location in the PowerShell?
- How to remove hidden files and folders using Python?
- How are files added to a zip file using Python?
- How to unzip all zipped files in a Linux directory?
- How to get the list of shared folders using PowerShell?
- How to get the list of empty folders using PowerShell?
