Unzip a file on your PowerShell command-prompt may come in handy sometimes, even on your Windows 11 . 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.
In this post you’ll learn how to use Expand-Archive to unzip files with PowerShell on Windows 11, or System.IO.Compression.ZipFile and the .NET 4.5+ Framework PowerShell command-prompt. This may come in handy sometimes, even in Windows 11 and Windows Server.
Extracting the contents of a zip archive using PowerShell Expand-Archive
To extract the contents of a zip archive file in PowerShell 5.0, you have the 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:
PS C:\Users\janreilink> Compress-Archive -Path C:\Users\janreilink\Downloads\OpenSSH -DestinationPath C:\Users\janreilink\Archives\openssh.zip
This command packages the files in C:\Users\janreilink\Downloads\OpenSSH and places the .zip archive file in C:\Users\janreilink\Archives.
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"
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! 🙂
In this post you learned how to use Expand-Archive to unzip files with PowerShell on your Windows 11 / 10 workstation, or System.IO.Compression.ZipFile and the .NET 4.5+ Framework. Also the usage of 7-Zip is touched. Check here how to install 7-Zip silently (including WinGet – Windows Package Manager).
This may come in handy sometimes, even in Windows 11 and Windows Server.
Summary
- You can unzip a file in PowerShell using the Expand-Archive cmdlet, available in PowerShell 5.0 and .NET 4.5+.
- Alternatively, you can use System.IO.Compression.ZipFile for unzipping with .NET Framework.
- 7-Zip also offers command-line options to unzip files or create archives easily.
- Remember to check your PowerShell version with $PSVersionTable to ensure compatibility with Expand-Archive.