PowerShell: How To Manage ZIP Archives

Introduction

Compressed folder ZIP archives have been introduced in Windows XP to allow users to collect multiple files together into a single .ZIP file that is represented on the file system as archives compressed using the standard ZIP format.
The Windows UI allows users to easily create and manage compressed archives, but that's also possible by using Windows PowerShell: as for many other scenarios, the ability to automate tasks such as the creation of a ZIP archive could be very useful.

There are two distinct ways to create and manage ZIP files using PowerShell, depending on the fact that you could be using a PowerShell version lower than 5 or a version equal to 5 or higher, therefore the first step to take is checking the available PowerShell version on the system: open a PowerShell window, type $PSVersionTable, hit ENTER and look at the value of the PSVersion field. Now you can proceed accordingly.

PowerShell version lower than 5 (.NET Style)

PowerShell version 5 and higher

PowerShell version 5 has introduced a new module named Microsoft.Powershell.Archive which contains cmdlets that let you create and extract archive or ZIP files: the Get-Command cmdlet with the -Module parameter allows you to see the cmdlets available within that module.

The available cmdlets are

  • Compress-Archive: creates a compressed archive, or zipped file, from specified files and directories
  • Expand-Archive: extracts files from a specified archive (zipped) file

Let's take a look at each of them.

Compressing files into an archive

The Compress-Archive cmdlet creates a compressed, or zipped, archive file from one or more specified files or directories. An archive packages multiple files, with optional compression, into a single zipped file for easier distribution and storage; an archive file can be compressed by using the compression algorithm specified by the CompressionLevel parameter. The Compress-Archive cmdlet uses the Microsoft .NET API System.IO.Compression.ZipArchive to compress files. The maximum file size is 2 GB because of a limitation of the underlying API.