SEARCH

BYTE SIZE TIPS / QUICK, HELPFUL TIPS

How-To Geek

How to Extract ZIP Files Using PowerShell

image

Windows includes the ability to unzip archives and even exposes this functionality programmatically via COM. Here is how to do it.

How to Extract ZIP Files Using PowerShell

We have seen this question asked numerous times on Stack Overflow and forums alike but most of the time people recommend using the PowerShell Community Extensions or a legacy command line application. Truth be told, its not actually all that hard to do in PowerShell.

$shell = new-object -com shell.application
$zip = $shell.NameSpace(“C:\howtogeeksite.zip”)
foreach($item in $zip.items())
{
$shell.Namespace(“C:\temp\howtogeek”).copyhere($item)
}

Hardcoding values isn’t really ideal so lets make it into a quick function.

function Expand-ZIPFile($file, $destination)
{
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file)
foreach($item in $zip.items())
{
$shell.Namespace($destination).copyhere($item)
}
}

Then we can simply use the function like this:

Expand-ZIPFile –File “C:\howtogeeksite.zip” –Destination “C:\temp\howtogeek”

Remember to add this  to your Windows PowerShell profile, so that you don’t need third-party libraries for functionality that is already included out of the box.

  • Rick P.

    Instead of auguring about Windows 8, maybe this would be a better use of one's time. smile)

    Click on the Link above.

  • Iszi

    I think I'll still prefer 7-Zip over PowerShell for this. Nice to know the capability is built-in to the OS though.

  • Netpilot

    @raphoenix Anyone can find books on the subject. The trick is to find a good, current one. Do you own any of those books or do you recommend one or two over the others?

    @Iszi I agree that I wouldn't use the example by itself instead of 7-Zip, but If you are going to do something further to the unzipped files in a larger PowerShell script, the code snippet will be really useful.

  • Rick P.

    @Netpilot

    PowerShell is such important program included with Windows 7 and 8, I feel it is extremely important to learn the program.

    Yes I own PowerShell books.

    Recommend the 5 Star Books in the Amazon Link posted above.

    May be we should have a Topic Named "What are you reading this Summer ?". smileysmiley

  • David Ruhmann

    This PowerShell unzip ability is further enhanced by the optional second parameter for the CopyHere function.

    I use vOption = 16 in my scripts. (Yes to All prompts)

    PowerShell (New-Object -COM Shell.Application).NameSpace('DESTINATION').CopyHere((New-Object -COM Shell.Application).NameSpace('ZIPFILE.zip').Items(), 16);

  • Iszi

    So, now that I've warmed up to PowerShell more recently, I actually ended up using this bit the other day to talk a Server Admin out of installing 7-Zip on a system that needed a command-line utility to extract a ZIP file in a batch job.

    (I have zero objections to 7-Zip personally - in fact, I stated my preference for it earlier in this thread - but professionally, part of my job is ensuring that systems adhere to the security principle of Least Functionality. That is, essentially, you should avoid installing additional software whenever possible.)

    A limitation I did find is that the command doesn't seem to be able to handle ZIP files which have an internal folder structure. Fortunately, that was not an issue for this particular use case.

Taylor Gibb is a Microsoft MVP and all round geek, he loves everything from Windows 8 to Windows Server 2012 and even C# and PowerShell. You can also follow him on Google+

  • Published 06/13/13

RECOMMENDED POSTS

Enter Your Email Here to Get Access for Free:

Go check your email!