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:

8 comments:

  1. Hi ... great script ...

    What if I want to zip a folder and all subfolders?

    ReplyDelete
    Replies
    1. Hi Autun,

      Thanks for your feedback. To answer your question, the code should work that way as written. It will make the determination for you if the file system object you're trying to zip up is a file or a folder. If it's a folder, it should pack up anything with that folder, including subfolders and their contents.

      Delete
  2. Could you please include a switch to target only specific types of files on this folder? eg. only *.txt/

    ReplyDelete
    Replies
    1. Hi AskVP,

      Sounds like it could be a good enhancement. I'll look into it over the next couple of days and post an update if I can get it to work. Thanks for the suggestion.

      Delete
    2. Hi AskVP,

      I worked through some options for your suggestion and have decided not to post a change to my original script. It would require significant alteration to do what you're asking. However, I'd encourage you to give it a go with your own copy of the code; it's definitely doable. Please reach out if you run into any issues you think I might be able to assist with.

      Delete
  3. Hi Bryan,

    I've found your script via Google and want to use it to zip a folder via a scheduled task.

    I'm testing the script from the PS CLI and I always seem to get this error:

    PS C:\scripts> .\ZipFolder.ps1
    Missing closing ')' in expression.
    At C:\scripts\ZipFolder.ps1:30 char:1
    + <<<< [string]$target,
    + CategoryInfo : ParserError: (CloseParenToken:TokenId) [], ParseException
    + FullyQualifiedErrorId : MissingEndParenthesisInExpression

    I've copied and pasted the script so I'm puzzled where the missing ) might be...

    The server is Windows Server 2008 with .Net 4.5.1 installed.

    Thanks,

    Chris.

    ReplyDelete
    Replies
    1. Hi Chris,

      Sorry to hear you're having troubles with the script. I'm not sure what might be causing the issue; I just pasted the code in the article into a new .ps1 file and it worked properly, so I don't think it's in the sample.

      Maybe try using the PowerShell ISE instead of the CLI. At least you'll be able to set breakpoints and step through the code to help you find the issue. On my workstation, the .exe can be found here: %windir%\sysWOW64\WindowsPowerShell\v1.0\PowerShell_ISE.exe.

      Hope that helps lead you down the right path to getting it working.

      Delete
  4. Thanks for the reply Bryan.

    PS seems much happier when I have each of the params on a single line eg

    [Parameter(Mandatory=$true,Position=0)][string]$target,

    Also found I needed to upgrade from PS v1 (via v2) to v3.

    Thanks for publishing your script.

    ReplyDelete