Create ZIP archives with PowerShell and the Shell.Application COM object

If you search the web, then you’ll find no shortage of PowerShell scripts for creating ZIP archives. Most of these solutions use the .NET compression classes, where some scripts simply call command-line versions of third-party archiving commands like WinRar, 7Zip, or even the venerable WinZip. That’s great if it works for you. However, all of these should be unnecessary by the time PowerShell 5.0 rolls around, but we’ll need to roll out our own zip tools for now.

This is especially true when you’re working with Desired State Configuration (DSC) and a pull server. If you want to deploy custom resources to managed nodes, then the resources need to be zipped up and copied to the pull server. Although there’s a checksum step that needs to be performed, but that’s not what I’ll cover in this article today. There are reports that zip files created with the .NET compression classes don’t work properly in a pull server situation. The suggestion is to use Windows Explorer to create the zip file. Alternatively, you can use the Shell.Application COM object, which I’ll show you how to use in this article.

Here’s the function.

Although I wrote the function with DSC in mind, you can use it to create a zip archive for any folder. It’s very important to note that the specified folder that you’re archiving will be a top-level folder in the zip archive. To use, all you need to do is specify the target folder and the name of the zip file to create. The zip file must not already exist, otherwise you’ll receive a warning. I’ve included a –Force parameter to delete the zip file if it already exists so that you can create a new one. I’ve also included simple support for –WhatIf.

Here’s how you might use my solution to backup a folder.

Creating a zip archive with PowerShell. (Image Credit: Jeff Hicks)
Creating a zip archive with PowerShell. (Image Credit: Jeff Hicks) 

With –Passthru, you may see the zip file that shows the initial size. Although there’s supposed to be flags that you can use to suppress the copy dialog box, I can’t get any of them to work on Windows 8.1. In my research, it appears that these flags may or may not work by design. However, the dialog may be helpful for larger folders.

You can use this function for any module that’s installed on your system. This is very handy if you’re developing a new module and want to package it.

The ModuleBase property is the top-level folder for the module. The example above will create a zip file for the module using the module name on the E:\ drive.

Finally, here’s an example where I use the function to create zip files for all the custom DSC resources installed on my computer.

My sample skips anything under System32, which should be the out-of-the-box resource modules like PSDesiredStateConfiguration. Note that I am using the .NET format operator, -f, to create the output file name.

This is often an easier approach instead of relying on variable expansion, or God forbid, concatenation.

If you’re getting started with DSC, then you might want to take a look at my fundamentals course at Pluralsight. If you’re looking to create PowerShell tools and modules, then be sure to take a look at Learn PowerShell Toolmaking in a Month of Lunches. In any event, I hope this function helps until PowerShell 5.0 hits your desktop.

Related Topics:

  • PowerShell

    Don't have a login but want to join the conversation? Sign up for a Petri Account