Compress & Extract ZIP Archives with PowerShell


PowerShell 5.0 includes a module called Microsoft.PowerShell.Archive, which enables you to compress and extract ZIP files from the command line, or inside your PowerShell scripts. If we use the core PowerShell Get-Command, we can dynamically discover the commands that are available inside this module. You should already be familiar with the Get-Command command if you’re a regular PowerShell user, but if not, make sure that you remember it! It will save you lots of time by discovering available commands!

PowerShell Archive Module

Compressing Archives

You can create ZIP archives by using the Compress-Archive command. The -Path parameter allows you to specify the source files that will be added to the archive, and the -DestinationPath parameter allows you to specify the path where the archive will get created. You can optionally specify the -CompressionLevel parameter, which accepts values of: NoCompression, Optimal, or Fastest. There’s also an -Update parameter that allows you to add / update files to an existing ZIP archive, and the -Force parameter will overwrite the destination archive, if it already exists.

Compress-Archive Cmdlet

Compress-Archive Cmdlet

Extracting Archives

The Expand-Archive command is used to extract a ZIP archive. This command provides a -Path parameter, to specify the path to the ZIP file that will be extracted. There is also a -DestinationPath parameter, which specifies the folder that the ZIP file will be extracted to.

Example Code

Putting everything together, that we talked about above, here is an example script that compresses / expands files in the c:\temp directory.