In this post, we will learn how to zip a file using the Compress-Archive PowerShell cmdlet.
How to zip a File using PowerShell?
In PowerShell, you can utilize the Compress-Archive command to compress the files and folders in a particular location. The Compress-Archive command compresses or zips multiple files or folders from different sources to a specific destination.
In order to use the Compress-Archive command, follow the below-given syntax.
Syntax
The syntax to compress files in a zipped form in PowerShell is:
Here, <SourcePathofZipFile> represents the actual path of the files, and the <DestinationPath> points to the destination path where you want to zip them.
Example 1
Let’s try an example to compress files of a particular folder by using PowerShell. Here, in drive E, we have a folder named 2 that contains multiple files, including three documents, an image, a text file, and a PowerPoint Presentation. We want to zip this folder in the same PowerShell tutorial directory:
In the PowerShell window, type the following Compress-Archive command:
After executing the given command, switch to File Explorer and verify the presence of the zipped folder in E:\Powershell tutorial:
Example 2
Let’s try another example to compress files from different sources to a specified destination by using PowerShell.
Now we will fetch multiple files from different sources and zip them. Here, we will choose an image file named img from drive E:\Powershell tutorial\1:
Next, we will select a document Blog1 from drive D:\New folder\:
Type the following Compress-Archive cmdlet in the PowerShell window to zip the selected multiple files:
Lastly, we will verify the presence of the zipped folder Tutorials that is created in folder E:\Powershell tutorial:
We have provided all the essential information related to zipping files in PowerShell by using Compress-Archive cmdlet.
Conclusion
In PowerShell, the Compress-Archive command is used to zip the files or folders. The syntax of the Compress-Archive command is given as Compress-Archive -Path <SourcePathofZipFile> -DestinationPath <DestinationPath>, where <SourcePathofZipFile> represents the source path of the zipped file and the <DestinationPath> points to its destination path. This article discussed how to zip a file using the Compress-Archive PowerShell cmdlet.