Use PowerShell to Extract Zipped Files


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 

Comments (10)

  1. straymoment says:

    Try unzipping the archive using a program and then place the output into a temporary set of files. Then separately process each compressed file using a batch command using the script shown above. -straymoment

  2. LN says:

    Can you please let me know, How do I extract a particular folder from zip file.

  3. jeremy says:

    Is there a way to force overwrite on exist? I realize I could test-path > remove-item, etc, but I’m wondering if the method used above has that capability?

  4. Manny says:

    Nice and simple, thanks. Works perfectly.

  5. xyz says:

    Is there any code available in powershell to compress large block of script??

  6. Joe says:

    It’s ‘espresso’.

  7. Scott says:

    I am new to using powershell and I am running into this error when trying to use the following command. I have searched on google for a while now and have not been able to find a solution.

    PS C:UsersThisGuy > Add-Type -assembly "system.io.compression.filesystem"

    Add-Type : Cannot add type. The assembly ‘system.io.compression.filesystem’ coud not be found.
    At line:1 char:9
    + Add-Type <<<< -assembly "system.io.compression.filesystem"
    + CategoryInfo : ObjectNotFound: (system.io.compression.filesyste
    m:String) [Add-Type], Exception
    + FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands
    .AddTypeCommand

    Add-Type : Cannot add type. One or more required assemblies are missing.
    At line:1 char:9
    + Add-Type <<<< -assembly "system.io.compression.filesystem"
    + CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationExc
    eption
    + FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Comman
    ds.AddTypeCommand

  8. Scott says:

    I figured out the issue. My Windows Management Framework was on an older version so I bumped it up to the next one and everything worked out just fine..

  9. michael says:

    >SK, that is all there is to using Windows PowerShell to extract zipped files.

    Did you manage to keep a straight face when you typed that?

    Seems to me if there’s a Compress-Archive command there should just be a Decompress-Archive analog command. But then again, obscurity seems to be Powershell’s forte.

    1. Erlend says:

      Michael:
      You mean Expand-Archive? Yup, it’s there.
      Get-Command *archive

Skip to main content