Unzip a file in PowerShell

Automating configurations on remote machines can sometimes make simple things interesting. In this specific scenario, I needed to use WinRm to Upload a file to a Virtual Machine (VM) on Microsoft Azure. Then I needed to unzip the file and finally go ahead with the configuration of the said software.

Searching the web gave me an appreciable amount of creative ways to go about unzipping files. This was by far the simplest approach I found. Keep in mind that it requires .NET 4.5.

$sourceFile = 'C:\assets\Microsoft.Azure.ServiceFabric.WindowsServer.5.3.204.9494.zip'
$targetFolder = 'C:\Microsoft.Azure.ServiceFabric.WindowsServer'

[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
[System.IO.Compression.ZipFile]::ExtractToDirectory($sourceFile, $targetFolder)

4 responses to Unzip a file in PowerShell (The Easy Way)

  1. 

    Hey, just wondering if you knew about the Expand-Archive cmdlet? So your example would look like this:

    Expand-Archive -Path $sourceFile -DestinationPath $targetFolder

    Liked by 1 person

  2. 

    Expand-Archive is only available in new versions of PS, using the .NET component seems to work great for older versions.

    Like

  3. 

    I was hosed using Expand-Archive on a target machine that had PowerShell 4. Thanks for posting this. It works well.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.