You are here: Sysadmins of the North » Code base » PowerShell » How to unzip a file in PowerShell

How to unzip a file in PowerShell

Unzipping a file on your PowerShell command line may come in handy sometimes, even on your Windows 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.

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

Expand-Archive D:\file.zip -DestinationPath C:\tempCode language: PowerShell (powershell)

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.

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"Code language: PowerShell (powershell)

How to: Compress and Extract Files

Show Your Support

donate with Paypal

If you want to step in to help me cover the costs for running this website, that would be awesome. Just use this link to donate a cup of coffee ☕($10 USD or €10 EUR for example). And please share the love and help others make use of this website. Thank you very much! <3 ❤️

1 thought on “How to unzip a file in PowerShell”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top