Compresses a folder path to a ZIP Archive
   This function compresses files in a folder path to a ZIP file using the native Windows ZIP library no 3rd party DLLs needed
   Zip-Folder -Source D:\foldername -Destination D:\fullpath\
   Zip-Folder -Source D:\fullpath\onefile\or\more\*.ps1 -Destination D:\fullpath\
# Source = Files to ZIP normally should be a folder but it handles a single file or multiple files
# Destination = D:\fullpath\ (with ZIP extension)

# Setup some global variables
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
$includeBaseDirectory = $false # if true creates a folder within a folder
$tempName = "ZIP"+(Get-Date -Format MM-d)
[System.IO.FileInfo]$tempFolder = $env:TEMP+"\"+$tempName

# Load the built in ZIP Assembly from Windows
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
function Zip-Folder ($Source, $Destination)
        if (Test-Path $Destination) {Write-Error "Zip file by that name already exists in the destination folder" -ErrorAction Stop}
        if ((Get-Item $Source).PSIsContainer -ne $true ) 
            Write-Warning "Source is not a folder creating one for you"
            New-Item -Path $tempFolder -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
            Copy-Item $Source -Destination $tempFolder -Force
            $Source = $tempFolder

    # Now create the zip file
    Remove-Item $tempFolder -Recurse -Force -ErrorAction SilentlyContinue

One thought on “Zip-Folder

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s