Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
What's with the requirement to download 7zip from SF? Wy can't you install/copy from an internal source? –  longneck Nov 8 '12 at 21:33
    
@longneck The PS script I am producing is going to be handed out to customers; so the script has to be entirely self contained. I could host a zip utility ourselves, but that is another headache I don't want (legalese). I was just hoping there was a simple Cmdlet that could do this that got intro-ed in 2012. –  vcsjones Nov 8 '12 at 21:54
    
If you are handing something out to customers why not build a self-extracting archive? –  Zoredache Nov 9 '12 at 7:01
    
@Zoredache Well, that seemed like more work initially (our build system is already building ZIPs), I was hoping it would just be "real quick". –  vcsjones Nov 9 '12 at 14:04

1 Answer 1

up vote 10 down vote accepted

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)
share|improve this answer
    
I just looked through System.IO.Compression and ZipFile isn't there. Is there an update to 4.5 that adds it? –  Mike Nov 9 '12 at 13:46
    
It seems this was introduced in 4.5, but not present in 4.0. If you have 4.5 you should have it. –  Peter Hahndorf Nov 9 '12 at 13:54
    
This worked wonderfully. Thanks a lot! –  vcsjones Nov 9 '12 at 14:18
4  
It's about damn time that there's a native solution for this. –  MDMarra Nov 9 '12 at 15:49
    

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.