How to

How to Zip (and Unzip) Files Using PowerShell

How to Zip (and Unzip) Files Using PowerShell

ZIP file format reduces file size by compressing it into one file. This process saves disk space, encrypts data, and makes it easy to share files with others. Here’s how to zip and unzip files using PowerShell.

How to Zip Files Using PowerShell

Let’s start by compressing a few files into ZIP file archives using the Compress-Archive cmdlet. It takes a path to whatever file you want to compress – many files separated by commas – and archive it at the destination you specify.

First, open PowerShell by searching for it from the Start menu and then typing the following commands, replacing <PathToFiles> and <PathToDestination> with the path to the file you want to compress and the name and folder you want, respectively:

Compress-Archive -LiteralPath <PathToFiles> -DestinationPath <PathToDestination>

1

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

Quotations around the path are only needed when the file path contains spaces.

Or, to zip the entire contents of the folder – and all its subfolders – you can use the following command, replacing <PathToFolder> and <PathToDestination> with the path to the file you want to compress and the name and folder you want. to go to, respectively:

Compress-Archive -LiteralPath <PathToFolder> -DestinationPath <PathToDestination>

2
In the previous example, we placed the path to a directory with many files and folders in it without specifying individual files. PowerShell takes everything in the root directory and compresses it, subfolders and everything.

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

Above, we discuss how to enter the root directory and all files and subdirectories when creating archive files. However, if you want to exclude the root folder from the Zip file, you can use wildcards to remove it from the archive. By adding an asterisk (*) to the end of the file path, you are telling PowerShell just to take what is in the root directory. It should look like this:

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

3

Next, say you have a folder with many different file types (.doc, .txt, .jpg, etc.) but only want to compress all file types. You can tell PowerShell to archive it without touching the others explicitly. The command notation will look like this:

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

4
Root subdirectories and file folders are not included in the archive with this method.

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

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

5
Root subdirectories and file folders are not included in the archive with this method.

Even after the archive is complete, you can update the existing zip file by using the Update parameter. This allows you to replace the older version of the file in the archive with a newer one that has the same name, and add the file that was created in the root directory. It will look like this:

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

6

How to Unzip Files Using PowerShell

Besides being able to zip files and folders, PowerShell has the ability to unzip archives. The process is even easier than pressing it; all you need is a source and destination file for data that is ready to be unzipped.

Open PowerShell and type the following commands, replacing <PathToZipFile> and <PathToDestination> with the path to the file you want to compress and the name and folder you want, respectively:

Expand-Archive -LiteralPath <PathToZipFile> -DestinationPath <PathToDestination>

7
The destination folder specified to extract files into will be filled with the contents of the archive. If the folder does not exist before unzipping, PowerShell will create a folder and place content into it before unzipping.

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

In the previous example, if we quit – DestinyPath, PowerShell will create a “Archive” folder in the “C: \ Users \ brady” path and extract files from the archive into a folder.

8

If the folder is already in the destination, PowerShell will return an error when trying to unzip the file. However, you can force PowerShell to overwrite data with new ones using the -Force parameter.

You only have to use the -Force parameter if old files are no longer needed, because this will permanently restore files on your computer.

Related posts

How to Troubleshoot Your Zoom Calls

Howto

How to Stop Facebook from Tracking You in Firefox

Howto

How to set up Alexa on Amazon Echo

Howto

How to Use the Xbox One Controller on a PC 2020 Guide

Howto

How to Open a Dropbox Folder in File Explorer or Finder

Howto

How to Sideload Apps and Games on your Oculus Quest

Howto

How To Install A Brush In Photoshop

Howto

How to Quick Steps to Improve PC Performance

Howto