Compress Directories with a PowerShell Script

When I was working on publishing Chrome Extensions for my Online Flashcards application I ran into the need to package up multiple directories in an efficient manner. I decided to compress the directories with a PowerShell script so that I could process everything in one bulk transaction.

Getting Started – Script Header Template

I include a standard header template in all of my PowerShell scripts. It contains information that I can refer back to later on when I'm editing the script or when I'm trying to remember exactly what my intent was when I originally created the script.

<#
    Title:       Compress specified directory to destination file
    Author:      Nat Thompson - nat@obsidianriver.com
    Create Date: 06/19/2019
    Purpose:     Compress specified directory as package to upload

    Version History:
        Version: 1.0
        Date:    06/19/2019
        Author:  Nat Thompson - nat@obsidianriver.com

        Change Description:

            Original script.  Compress directory to destination directory.
#>

I also include a version history in my header. This is complete with a version, date, author email and a Change Description. You'll be amazed at how useful it is to review the evolution of your PowerShell scripts over time.

The Script Parameters

# Input parameters for source dir, destination dir
param (
    [string]$sourceDir,
    [string]$destinationDir
)

Compressing files with my PowerShell script requires that I accept two parameters: a parent directory and a destination directory. I set those parameters up as string inputs and include a comment above them to explain what they will be used for.

Communicating Status

# let the user know what we are processing
Echo "Processing directories in $sourceDir"

Next, I write out a console message for the user to let them know what is being processed. This helps keep the user informed that things are happening behind the scenes to keep them from thinking that the PowerShell script might be frozen.

Looping Through All The Work

#loop through directories
foreach ($dir in Get-ChildItem $sourceDir -Directory) { 
    # Compress directory
    Echo "Compressing $dir"
    Compress-Archive -Path $sourceDir\$dir $destinationDir\$dir.zip -Update
}

Now we're ready to loop through the sub-directories and perform the compression. Since my need was to compress each directory found into an individual file that's exactly what this loop does.

The foreach loop iterates through every child object in the results of the Get-ChildItem function for the $sourceDir location. The -Directory flag filters out all objects except for directories.

I write out a message to the console with the Echo command to let the user know which directory is currently being compressed.

I then issue the Compress-Archive command and include the source directory and destination directory as parameters. I include the -Update flag to let the PowerShell script know that if a zip file with the name I'm using already exists to update it with the new zip file.

The Big Finish – Compress Directories with PowerShell Script Complete

# all done, let the user know
Echo "Processing complete!"

Just to make things perfectly clear I write out a console message to the user confirming that the compression of directories with the PowerShell script is complete.

I've packaged this script along with a ReadMe.txt file for you to download free of charge. Use the product link below to get your copy today.