Use PowerShell to Extract Zipped Files

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to extract zipped files. Hey, Scripting Guy! Question Hey, Scripting Guy! I need to be able to work with zipped files from time-to-time. Often I store files in a zip archive so they are portable. I know how to copy the .zip archive from one place to another with Windows PowerShell, but I cannot seem to figure out how to unzip the archive. Can you help me? I would be ever so grateful. —SK Hey, Scripting Guy! Answer Hello SK, Microsoft Scripting Guy, Ed Wilson, is here. This morning I got out my bag of espresso beans and set up my rotary bean grinder. I am thinking I will make some espresso this afternoon…or maybe tomorrow afternoon. I have an old-fashioned, stovetop, double-boiler. It is like one I bought a long time ago when I was in Naples, Italy. It is ridiculously simple, and does an excellent job. I put the water in the bottom and super finely ground beans in the middle, and after a short time, the espresso appears in the top. The only trick is ensuring that I get the right amount of water in the bottom. I sometimes also make cappuccinos on Saturday mornings, and I have a hand milk frothier that I use for that. I love using manual tools when I have time, or when I want to take time for an exceptional occurrence. However, for things that occur more than once or twice a month, I want to automate them—big time. Maybe one day, someone will invent a PowerShell powered teapot. To extract all files from a .zip archive file, I use the ExtractToDirectory static method from the [io.compression.zipfile] .NET Framework class. To use this class, I need to add the System.IO.Compression.FileSystem assembly to my Windows PowerShell console or to the Windows PowerShell ISE. To add the assembly, I use the Add-Type cmdlet and specify the –Assembly parameter. This command is shown here:

Add-Type -assembly “system.io.compression.filesystem”

The command to extract the zipped files to a folder is:

[io.compression.zipfile]::ExtractToDirectory($BackUpPath, $destination)

Here are a few things to keep in mind:

  1. The first parameter I call ($BackUpPath) must point to a specific zipped file.
  2. The second parameter (the one I call $destination) must point to a folder.
  3. Both of these parameters are strings. Therefore, I cannot use a ziparchive object or a directoryinfo object as input types.
  4. The extraction does not include the root folder.

My complete script is shown here:

$BackUpPath = “C:backupfso.zip”

$Destination = “C:recovered”

Add-Type -assembly “system.io.compression.filesystem”

[io.compression.zipfile]::ExtractToDirectory($BackUpPath, $destination)

When I go to my C:recovered folder, I see that all of the files from the fso.zip folder are now present. SK, that is all there is to using Windows PowerShell to extract zipped files. Zip Week will continue tomorrow when I will talk about zipping and emailing an archived folder. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon