Visual Studio Build Events Script – Part 2: Automating Release Builds with PowerShell Zip Archiving

How to automatically zip your Release build output with a PowerShell script triggered by Visual Studio post-build events

Posted by Hüseyin Sekmenoğlu on March 01, 2019 Tooling & Productivity

🧩 Overview

I wrote this script along with the previous one but it has a different purpose and approach, so I am sharing it in a separate article.

This time, the script is a PowerShell script that runs as a post-build event in Visual Studio.

Its main job is to take the compiled executable (.exe) and dynamic link library (.dll) files from a desktop project when built in Release mode and compress them into a zip archive automatically.


🗂️ Naming the Zip Archive

The zip file name is composed of several elements:

  • Project name (target name)

  • Current date and time in the format yy.MM.dd HH.mm.ss

  • User name (hardcoded here as "Huseyin")

For example, the output zip could look like:

MyProject 25.08.23 14.30.15 Huseyin.zip

This makes it easy to keep track of different builds and who created them.


🤔 Why Do This?

Some may say this is unnecessary because version control systems like Git exist.

However, many teams still struggle with inconsistent builds, where features present in one version disappear or stop working in another.

This script provides a simple, visible artifact of each build which can be shared or archived.


🛠️ How to Use It in Visual Studio

In Visual Studio, go to your project's Post-Build Event Command Line and add the following command:

powershell -ExecutionPolicy ByPass -file "c:\Post-Execute.ps1" -ConfigurationName "$(ConfigurationName)" -TargetName "$(TargetName)"

Make sure the Post-Execute.ps1 script is saved in C:\ or adjust the path accordingly.


📝 The PowerShell Script (Post-Execute.ps1)

param (
    [Parameter(Mandatory=$true)][string] $ConfigurationName,
    [Parameter(Mandatory=$true)][string] $TargetName
 )

$User="Huseyin"

function zipFiles([string] $sourceFileName)
{   
    Add-Type -Assembly System.IO
    Add-Type -Assembly System.IO.Compression.FileSystem

    $sourceFolder="\\192.168.2.232\Shared\_Output\" + $sourceFileName + "\"
    $DateStr = (Get-Date).ToString("yy.MM.dd HH.mm.ss")
    $FileName = $TargetName + " " + $DateStr + " " + $User + ".zip"
    $zipFileName = $sourceFolder + $FileName

    $releaseFolder = $sourceFolder + "Temp"
    $exeFilter = $sourceFileName + ".exe*"
    $dllFilter = $sourceFileName + ".dll*"

    if (-not (Test-Path $releaseFolder)) 
    { 
        New-Item -ItemType Directory -Force -Path $releaseFolder
    }

    Get-ChildItem $sourceFolder -Filter $exeFilter | Copy-Item -Destination $releaseFolder -Force
    Get-ChildItem $sourceFolder -Filter $dllFilter | Copy-Item -Destination $releaseFolder -Force

    try
    {
        [System.IO.Compression.ZipFile]::CreateFromDirectory($releaseFolder, $zipFileName)
    }
    catch [System.IO.IOException] 
    {
        Write-Output ((Get-Date) + ' | ' + $_.Exception.Message )
    }

    Remove-Item -Path $releaseFolder -Recurse -Force -ErrorAction SilentlyContinue
} 

if ($ConfigurationName -eq "Release") {
    zipFiles -sourceFileName $TargetName
}

📌 How the Script Works

  1. Parameters: The script takes two mandatory parameters from Visual Studio:

    • ConfigurationName (e.g., Debug or Release)

    • TargetName (the project or output name)

  2. User variable: A fixed user name is set to tag the zip file.

  3. Source and Temp folders:

    • It defines the source folder where the compiled files reside on a shared network location.

    • It creates a temporary folder inside to copy .exe and .dll files.

  4. File copying: It copies all .exe and .dll files that match the project name pattern into the temp folder.

  5. Zipping: Uses the built-in .NET zip compression method to create a zip archive named with project, date, time and user.

  6. Cleanup: Deletes the temporary folder after zipping.

  7. Conditional execution: The script runs only if the build configuration is Release.


💡 Benefits

  • Automatic packaging of release builds

  • Easy distribution and archiving

  • Timestamped build artifacts to track build history

  • Simple integration with Visual Studio build system


🚀 Next Steps

You can extend this script by:

  • Adding logging for success or failure

  • Uploading the zipped files automatically to cloud storage or a deployment server

  • Including additional files like configuration or documentation in the zip