Unzip File With Powershell in Server 2012 Core
I need to unzip a file with powershell. The typical way I've seen everyone do this is by automating the shell with a script.
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder.CopyHere($zipPackage.Items())
This isn't going to work for me, as Server Core doesn't have a shell, so there isn't one to automate. This gives an E_FAIL COM error.
Powershell doesn't seem to be able to do it on its own, and if I go 3rd party, I have to figure out a way to script getting the utility on to the server in the first place. 7-Zip was my go-to, but it doesn't seem like I can script the download and install of it. Sourceforge keeps spitting me back HTML files.
How can I completely script unzipping a zip file in Server 2012 Core?
Server 2012 comes with Dot.NET 4.5 which has System.IO.Compression.ZipFile which has a ExtractToDirectory method. You should be able to use this from PowerShell.
Here is an example.
First you need to load the assembly ZipFile is in:
[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null
Then extract the contents
[System.IO.Compression.ZipFile]::ExtractToDirectory($pathToZip, $targetDir)
Edit: If you have updated to PowerShell 5 (Windows Management Framework 5.0) you finally have native cmdlets:
Expand-Archive $pathToZip $targetDir