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)
Hey, just wondering if you knew about the Expand-Archive cmdlet? So your example would look like this:
Expand-Archive -Path $sourceFile -DestinationPath $targetFolder
LikeLiked by 1 person
I did not and did not find it while I was searching. Thanks for sharing, I hope it will help others recover cycles for other challenges.
LikeLike
Expand-Archive is only available in new versions of PS, using the .NET component seems to work great for older versions.
LikeLike
I was hosed using Expand-Archive on a target machine that had PowerShell 4. Thanks for posting this. It works well.
LikeLike