How to zip files using powershell - my own solution

  • Hello,
    as I struggled all day today to find out how to zip folders using Powershell I decided to post my solution, just in case anyone has any problems using Google as a source. The latter provides tons of solutions, but very often snippets and incomplete instructions. Overall I visited probably around 20-30 web pages.I won't mention MS documentation, so much confusion in names, version, compatibility, etc. So here is what you need:
    1) It's best to install .net 4.5 (or something above 4.0) using the full version. You probably need to search for the file NDP452-KB2901907-x86-x64-AllOS-ENU.exe
    2) Upgrade Powershell from 2.0 to 3.0 - this is actually part of the Windows Management Framework 4.0, download here:
    https://www.microsoft.com/en-us/download/details.aspx?id=40855
    Make sure you read the System Requirements.

    3) Write the PS script that will accept 2 parameters, 1) input folder to be zipped 2) result .zip file:

    # ========================================================================================================
    # Script : pc_compress.ps1
    # Descr. : Zip script for Powershell
    # Author : Richlion2
    # Date : 11/05/2017
    #
    # Prereq.: this script will zip some files. For this to work you may need .Net 4.5.2 and Powershell 3.0,
    #    found under Windows Management Framework 4.0
    #
    # [System.IO.Compression.ZipFile]::CreateFromDirectory($cdir , $cdest , 0 , $false)
    # $cdir is the directory you want to zip.
    # $cdest is the name of the zipfile you want to create.
    # $Compressionlevel can be set to Optimal, Fastest or NoCompression. => use 0
    # $includeBaseDir can be $true or $false.
    # ========================================================================================================

    Param(
    [string]$cdir,
    [string]$cdest
    )

    # Adds a Microsoft .NET Framework type (a class) to a Windows PowerShell session.

    Add-Type -assembly "system.io.compression"
    Add-Type -assembly "system.io.compression.filesystem"

    # To use .NET for zipping you first need to load the class assembly:
    [System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")

    # Make sure you don't create the ZIP file inside the folder being zipped, you will get
    # this message:
    # "The process cannot access the file 'c:\adm\adm.zip' because it is being used by another process."

    # Delete the old zip file first
    Remove-Item $cdest -force

    [System.IO.Compression.ZipFile]::CreateFromDirectory($cdir , $cdest , 0 , $false)

    4) Call your script directly from a CMD window or write a command prompt script:

    @echo off
    c:
    cd \adm
    powershell -ExecutionPolicy Bypass -command "& c:\adm\ps_compress.ps1 c:\adm\ c:\temp\adm.zip"

    REM Or
    REM powershell -ExecutionPolicy Bypass -command "& c:\adm\ps_compress.ps1 -cdir c:\adm\ -cdest c:\temp\adm.zip"

    This will zip c:\adm into c:\temp\adm.zip

    I hope this helps.
    Richard

Viewing 0 posts

You must be logged in to reply to this topic. Login to reply