Powershell

How to Zip a File in PowerShell?

The procedure of compressing multiple files in a single folder is called Zipping. Zipped files are used for keeping the related files together at a place and compressing them so that they can be easily shared with the intended receiver via web or any other source. Due to their reduced storage requirements, zipped files are considered perfect for archiving and play their part in encrypting data.

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:

Compress-Archive -Path <SourcePathofZipFile> -DestinationPath <DestinationPath>

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:

> Compress-Archive -Path 'E:\Powershell tutorial\2'-DestinationPath 'E:\Powershell tutorial\2.zip'

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:

> Compress-Archive -LiteralPath 'E:\Powershell tutorial\1\img.jpg', 'D:\New folder\Blog1.docx' -DestinationPath 'E:\Powershell tutorial\Tutorials.zip'

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.