Extracting Zip Files using PowerShell

July 17th, 2012

To help troubleshoot some SharePoint issues, I had a need to analyze some log files that were contained in multiple zip files; one log file per zip file. Since there were several hundred zip files to extract, I figured PowerShell could help! There are several posts I found with example scripts for how to perform this operation. I took pieces from many posts, added some COM object clean-up code, and wrapped this in a function. I hope it helps you in your scripting activities!

The code below is the function I’m using to extract a single zip file. It leverages the Windows Shell COM object (i.e. Windows Explorer) to extract the files by exploiting the fact that Windows Explorer treats zip files as folders. Therefore, the operation can leverage existing file copy methods between two folders.

Tags: , , , ,

3 Responses to “Extracting Zip Files using PowerShell”

  1. Loren Says:

    Very useful!! was looking for an alternative to 7zip calls as they don’t always seem to work with the options I need. One issue I seem to have is with the HideProgressDialog setting. Was does this switch need? tried the $HideProgressDialog=$true, but that didn’t work. Same for the $OverwriteExistingFiles switch.
    Very useful!

  2. Steve Mayes Says:

    Hi Loren,

    Glad to hear this script was useful to you!

    In the function above – I ended up using the [switch] parameter on those two input variables. Keep in mind that adding switch is really just another way to specify a boolean variable. But, you get the cool PowerShell features of specifying the parameter on the command line as a switch versus a true/false.

    You can use any of the methods below for calling the function. I’ll use HideProgressDialog as an example but the same methods apply to both HideProgressDialog and OverwriteExistingFiles.

    # These commands both set the HideProgressDialog variable to $true - thus hiding the progress dialog
    Expand-Zip "C:\test.zip" "C:\ExpandedFiles" -HideProgressDialog
    Expand-Zip "C:\test.zip" "C:\ExpandedFiles" -HideProgressDialog:$true
    
    # These commands both set the HideProgressDialog variable to $false - thus showing the progress dialog
    Expand-Zip "C:\test.zip" "C:\ExpandedFiles" # This will *not* hide the progress dialog, because I did not supply the switch
    Expand-Zip "C:\test.zip" "C:\ExpandedFiles" -HideProgressDialog:$false

    Hope this helps,
    Steve

  3. Evan Says:

    The copy flags operation is not working. No matter the flags specified, it will always display a progress dialog.

Leave a Reply