Unzip with Powershell

A lot of sites mention the below method to unzip files in Powershell. However in Window Server 2012 i was getting “You cannot call a method on a null-valued expression” at the .items()

// THIS CODE DOESNT WORK WITH WINDOWS SERVER 2012
function Extract-Zip
{
param([string]$zipfilename, [string] $destination)
$shell_app=new-object -com shell.application
$zip_file = $shell_app.namespace($zipfilename)
$dest = $shell_app.namespace($destination)
$dest.Copyhere($zip_file.items())
}

So, i found an alternative using .Net. It requires .Net Framework but i have this as a pre-requesite anyway.

function Extract-Zip 
{
param([string]$zipfilename, [string] $destination)
#Load the assembly
[System.Reflection.Assembly]::LoadWithPartialName(“System.IO.Compression.FileSystem”) | Out-Null
#Unzip the file
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfilename, $destination)
}

Originally published at www.nexusconcepts.com.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.