How to Zip (and Unzip) Files Using PowerShell

PowerShellHeader

the ZIP file format reduces the size of files by compressing them into a single file. This process saves disk space, encrypts data, and makes it easier to share files with others. Here’s how to compress and decompress files using PowerShell.

How to compress files using PowerShell

Let’s start by compressing certain files in a ZIP file archive using the Compress-Archive cmdlet. It takes the path to the files you want to compress (several files are separated by a comma) and archives them in the destination you specify.

First, open PowerShell by searching for it in the Start menu, then typing the following command, replacing and with the path to the files you want to compress and the name and folder you want it to go to, respectively:

Compress-Archive -LiteralPath -DestinationPath

Compress some files in PowerShell.

When providing the destination path, be sure to name the archive file or PowerShell will save it as “.zip” where you specify it.

Note: Citations around the path are only necessary when the file path contains a space.

Alternatively, to compress the entire contents of a folder – and all of its subfolders – you can use the following command, replacing and with the path to the files you want to compress and the name and folder where you want it to go, respectively:

Compress-Archive -LiteralPath -DestinationPath

Compress an entire folder and all of its contents.

In the previous example, we set the path to a directory containing several files and folders without specifying individual files. PowerShell takes everything inside the root directory and compresses it, subfolders and everything.

The Compress-Archive cmdlet allows you to use a wildcard character (*) to extend functionality even further. When using the character, you can exclude the root directory, compress only files in a directory, or choose all files of a specific type. To use a wildcard with Compress-Archive, you must use the -Path parameter instead, because -LiteralPath does not accept them.

Above, we explained how to include the root directory and all of its files and subdirectories when creating an archive file. However, if you want to exclude the root folder from the Zip file, you can use a wildcard to omit it from the archive. By adding an asterisk (*) at the end of the file path, you tell PowerShell only to type in what’s inside the root directory. It should look something like this:

Compress-Archive -Path C: path to file * -DestinationPath C: path to archive.zip

Zip the entire contents of a folder, without the root folder itself.

Suppose then that you have a folder with a bunch of different file types (.doc, .txt, .jpg, etc.) but you only want to compress all the types. You can tell PowerShell to archive them without explicitly touching others. The notation of the command would look like this:

Compress-Archive -Path C: path to file *. Jpg -DestinationPath C: path to archive.zip

Compress only specific file types from a folder.

Note: Subdirectories and files in the root folder are not included in the archive with this method.

Finally, if you want an archive that compresses only the files in the root directory – and all of its subdirectories – you will use the wildcard star-point-star (*. *) To compress them. It would look something like this:

Compress-Archive -Path C: path to file *. * -DestinationPath C: path to archive.zip

Only zip files in the root folder using the star-dot-star widlcard (*. *).

Note: Subdirectories and files in the root folder are not included in the archive with this method.

Even after the archive is complete, you can update an existing compressed file using the -Update parameter. It allows you to replace old versions of files in the archive with newer versions with the same names and to add files created in the root directory. It will look like this:

Compress-Archive -Path C: path to files -Update -DestinationPath C: path to archive.zip

Update an existing zip file using the -Update parameter.

How to decompress files using PowerShell

In addition to being able to compress files and folders, PowerShell has the ability to decompress archives. The process is even easier than compressing them; all you need is the source file and a destination for the data ready to decompress.

Open PowerShell and enter the following command, replacing and with the path to the files you want to compress and the name and folder where you want it to go, respectively:

Expand-Archive -LiteralPath -DestinationPath

Unzip an archive with the Expand-Archive cmdlet.

The destination folder specified to extract the files will fill with the contents of the archive. If the folder did not exist before decompression, PowerShell will create the folder and place the content there before decompression.

By default, if you omit the -DestinationPath parameter, PowerShell will decompress the content in the current root directory and use the name of the Zip file to create a new folder.

In the previous example, if we omit -DestinationPath, PowerShell will create the folder “Archive” in the path “C: Users brady” and extract the files from the archive in the folder.

If you omit the -DestinationPath parameter, PowerShell extracts the zip to its currently selected directory.

If the folder already exists in the destination, PowerShell returns an error when it tries to decompress the files. However, you can force PowerShell to replace the data with the new one using the -Force parameter.

You should only use the -Force parameter if the old files are no longer needed, as this will irreversibly replace the files on your computer.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.