Wednesday, August 21, 2013

Create zip files using PowerShell

Continuing my series of articles on handy PowerShell scripts, I'd like to take a look at creating zip files. If you missed either of the first two articles, you can get caught up on them both here.

Zipping a file or folder manually is obviously a trivial trivial process. However, there are many situations where you don't want to be doing it manually. You want it to happen overnight to move some backup data around, or to package up some files after a build, or to free up space on your server by compressing log files. There's also the case where zipping up some files is part of a process you need to do on a regular basis. It's not that you couldn't do it manually; it's just more efficient and reliable to automate it. This is the scenario that prompted me to come up with the script below.

I've been working on a little utility called wsubi for the past 8 months or so. The download I distribute for it is a zip file. As the project grew, each build required more care and time to ensure I had collected all the needed files, sample scripts, etc, before I created the download package. Perfect scenario to automate! Now, with the help of my PowerShell script, I just enter 'run ~build' from the application's console, and a few seconds later I have my latest build packed up and ready to go on my desktop.

Here's the code; I'm sure you'll be able to find many other good uses for it: