Using PowerShell to Create ZIP Archives and Unzip Files

Sometimes it can be useful to programmatically create zip archives or extract files from existing archives. Windows PowerShell 5.0 added two cmdlets for doing just that. The Compress-Archive cmdlet enables you to create new archives from folders or individual files and to add files to archives; Extract-Archive can be used to unzip files.

If you don’t already have PowerShell 5.0 or later installed on your systems, you can download the latest version of the Windows Management Framework from Microsoft’s website here.

Using PowerShell to Create Zip Files

Let’s start by using PowerShell to compress files in a new zip archive. All you need to do is use the -Path parameter to specify the folder you want to compress and the DestinationPath parameter to specify the name of the archive you want to create. The command below will zip the Invoices folder in the root C directory and create an archive called Invoices.zip in the Archives folder:

Compress-Archive -Path C:\Invoices -DestinationPath C:\Archives\Invoices

Alternatively, we could zip the files in the Invoices folder individually using -LiteralPath instead of Path. This command creates an archive with just the two files explicitly listed in the LiteralPath parameter:

Compress-Archive -LiteralPath C:\ Invoices\File1.txt, C:\Invoices\File2.txt -DestinationPath C:\Archives\Invoices -Force

Note that I added the -Force parameter to overwrite the archive that I created using the first command. Without the -Force parameter, you cannot overwrite existing archives and PowerShell will prompt you to add files to the archive instead.

To add files to an archive, use the -Update parameter. The command below adds all the files in the Invoices folder to my existing Invoices.zip archive:

Compress-Archive -Path C:\Invoices\* -Update -DestinationPath C:\Archives\Invoices

Optionally, you can use the -CompressionLevel parameter with one of three values: Optimal, NoCompression or Fastest. Optimal is the default setting if the -CompressionLevel parameter is not set; it uses the best compression available, but it might take longer than using Fastest. To create an archive with no compression, use the NoCompression value.

Using PowerShell to Unzip Files

Extracting files from an archive is even easier than creating one. All you need to do is specify the name of the archive and the destination folder for the unzipped files. The command below extracts the contents of the Invoices.zip archive to a folder named InvoicesUnzipped using the Expand-Archive cmdlet.

Expand-Archive -LiteralPath C:\Archives\Invoices.Zip -DestinationPath C:\ InvoicesUnzipped

The folder where you want to unzip the files doesn’t need to exist; Expand-Archive will automatically create the folder if needed. But if the files you want to unzip already exist in the destination folder, Expand-Archive will return an error. You can overwrite files in the destination folder by adding the -Force parameter to the command.

IT consultant and author specializing in management and security technologies. Russell has more than 15 years of experience in IT, he has written a book on Windows security, and he coauthored a text for Microsoft’s Official Academic Course (MOAC) series.