Click here to Skip to main content
15,036,441 members
Articles / Programming Languages / PowerShell
Technical Blog
Posted 15 Feb 2014

Tagged as


14 bookmarked

Create zip files using PowerShell

Rate me:
Please Sign up or sign in to vote.
4.73/5 (10 votes)
15 Feb 2014CPOL1 min read
A simple PowerShell script to automate zipping up files and folders.

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:

# |Info|
# Written by Bryan O'Connell, August 2013
# Purpose: Creates a .zip file of a file or folder.
# Sample: zipstuff.ps1 -target "C:\Projects\wsubi" -zip_to "C:\Users\Bryan\Desktop\wsubi" [-compression fast] [-timestamp] [-confirm]
# Params:
# -target: The file or folder you would like to zip.
# -zip_to: The location where the zip file will be created. If an old version
# exists, it will be deleted.
# -compression (optional): Sets the compression level for your zip file. Options:
# a. fast - Higher process speed, larger file size (default option).
# b. small - Slower process speed, smaller file size.
# c. none - Fastest process speed, largest file size.
# -add_timestamp (optional): Applies a timestamp to the .zip file name.
# By default, no timestamp is used.
# -confirm (optional): When provided, indicates that you would like to be
# prompted when the zip process is finished.
# |Info|

Param (





function DeleteFileOrFolder
{ Param([string]$PathToItem)

  if (Test-Path $PathToItem)
    Remove-Item ($PathToItem) -Force -Recurse;

function DetermineCompressionLevel
  $CompressionToUse = $null;

    "fast" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Fastest}
    "small" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Optimal}
    "none" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::NoCompression}
    default {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Fastest}

  return $CompressionToUse;

Write-Output "Starting zip process...";

if ((Get-Item $target).PSIsContainer)
  $zip_to = ($zip_to + "\" + (Split-Path $target -Leaf) + ".zip");

  #So, the CreateFromDirectory function below will only operate on a $target
  #that's a Folder, which means some additional steps are needed to create a
  #new folder and move the target file into it before attempting the zip process. 
  $FileName = [System.IO.Path]::GetFileNameWithoutExtension($target);
  $NewFolderName = ($zip_to + "\" + $FileName)


  md -Path $NewFolderName;
  Copy-Item ($target) $NewFolderName;

  $target = $NewFolderName;
  $zip_to = $NewFolderName + ".zip";


if ($timestamp)
  $TimeInfo = New-Object System.Globalization.DateTimeFormatInfo;
  $CurrentTimestamp = Get-Date -Format $TimeInfo.SortableDateTimePattern;
  $CurrentTimestamp = $CurrentTimestamp.Replace(":", "-");
  $zip_to = $zip_to.Replace(".zip", ("-" + $CurrentTimestamp + ".zip"));

$Compression_Level = (DetermineCompressionLevel);
$IncludeBaseFolder = $false;

[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" );
[System.IO.Compression.ZipFile]::CreateFromDirectory($target, $zip_to, $Compression_Level, $IncludeBaseFolder);

Write-Output "Zip process complete.";

if ($confirm)
  write-Output "Press any key to quit ...";
  $quit = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bryan O'Connell
Software Developer (Senior) SPR Consulting
United States United States
Bryan has been solving business problems with software and Agile methodologies for over twelve years. He has experience in all aspects of the software development lifecycle, having directed multiple projects from client initiation through product delivery, deployment, and growth as a team member, manager or independent contributor. Bryan is also a Certified Scrum Master and Manager.

Comments and Discussions

Questiongetting error while executing the script Pin
Member 1045395818-Feb-15 16:16
MemberMember 1045395818-Feb-15 16:16 
AnswerRe: getting error while executing the script Pin
Bryan O'Connell19-Feb-15 5:32
MemberBryan O'Connell19-Feb-15 5:32 

Thanks for giving my script a try; sorry you're having some issues getting it running. Not knowing what your system configuration is, I'd suggest having a look through this StackOverflow post and give a couple of these solutions a try.


General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.