Creating a ZIP Archive with PowerShell Version <5.0

The PowerShell command “Compress-Archive” comes pretty handy when it comes to creating a .zip file/archive. Sadly it only available since PowerShell version 5.0. which comes with Windows 10 (can be installed on Windows 8.1 and Windows Server 2012 R2 as well, though).
No worries, there is a solution as well which works with Powershell <5.0. The following function requires the output filename e.g. "C:\temp\MyOutputArchive.zip" and the full path of the directory to be archived eg. "C:\temp\ToBeArchived\".

1
2
3
4
5
function createZipFile($outputFileName, $sourceDirectory){
    Add-Type -AssemblyName System.IO.Compression.FileSystem
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory($sourceDirectory, $outputFileName, $compressionLevel, $false)
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.