How to unzip a file in PowerShell

Unzip a file on your PowerShell command-prompt may come in handy sometimes, even on your Windows 11/10 workstation. Use Expand-Archive for this, and all that is required is PowerShell 5.0+, or the .NET 4.5+ Framework to use System.IO.Compression.ZipFile.
Published on Wednesday, 30 August 2017

PowerShell Expand-Archive to unzip a file

Unzip a file in PowerShell 5.0, there is an Expand-Archive cmdlet built in:

Expand-Archive D:\file.zip -DestinationPath C:\temp

To zip, or compress files with PowerShell, you can use Compress-Archive.

Use the automatic $PSVersionTable variable, and check the PSVersion property, to get the PowerShell version. For example: $PSVersionTable.PSVersion. This should inform your whether Expand-Archive is available.

.NET-Framework System.IO.Compression

If you want a wrapper to unzip files with .NET Framework, then you can use the the System.IO.Compression namespace. This namespace contains classes that provide basic compression and decompression services for streams. You can also use these classes to read and modify the contents of a compressed zip archive file.

A simple way of using ExtractToDirectory from System.IO.Compression.ZipFile:

Add-Type -AssemblyName System.IO.Compression.FileSystem
function unzip {
  param( [string]$ziparchive, [string]$extractpath )
  [System.IO.Compression.ZipFile]::ExtractToDirectory( $ziparchive, $extractpath )
}

unzip "D:\file.zip" "C:\temp"

How to: Compress and Extract Files

7-Zip

On the command-line you can extract .zip files (unzip) or create an archive easily using 7-Zip too. See the following examples:

PS C:\Users\Jan Reilink> & 'C:\Program Files\7-Zip\7z.exe'

7-Zip 23.01 (x64) : Copyright (c) 1999-2023 Igor Pavlov : 2023-06-20

Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...] [@listfile]

Unzip on the command-line with 7-Zip

& 'C:\Program Files\7-Zip\7z.exe' x .\file.zip -oC:\Users\JanReilink\Documents\extract_folder

7-Zip commands and switches used are:

  • x: extract, or "eXtract files with full paths"
  • -o{Directory}: set Output directory

Zip files with 7-Zip (add files to archive)

& 'C:\Program Files\7-Zip\7z.exe' a -r file.zip d:\temp\folder_to_archive

The commands and switches used here are:

  • a: Add files to archive
  • -r: Recurse subdirectories for name search

So we recursive zipped "d:\temp\folder_to_archive" into "file.zip", nice! :) If you want to know how to install 7-Zip, see my Microsoft Deployment Workbench: silent installation of various applications post: 7zip silent install (including winget).

In this post you learned how to use Expand-Archive to unzip files with PowerShell on Windows 11/10 workstation, or System.IO.Compression.ZipFile and the .NET 4.5+ Framework PowerShell command-prompt. This may come in handy sometimes, even on your Windows 11/10 workstation.