Zip and passworded

Welcome Forums General PowerShell Q&A Zip and passworded

This topic contains 4 replies, has 3 voices, and was last updated by

1 year, 7 months ago.

  • Author
  • #69237

    Points: 0
    Rank: Member


    I need to zip and password protect a folder/file.
    For example source folder is C:\backup and destination will be C:\zipped

    I zipped folder by
    $source = C:\backup
    $destination = C:\zipped
    Add-Type -assembly ""
    [io.compression.zipfile]::CreateFromDirectory($source, $destination)

    but I don't know what to do next to passworded that file :/

  • #69240

    Points: 21
    Rank: Member

    From everything I can seem to find, you would need to leverage something like 7Zip to be able to set a password on the zip file. It looks like there's not even an option in the .NET framework without using the dotnetzip library to do this.

    PowerShell function to create a password protected zip file

    • #69268

      Points: 0
      Rank: Member

      @Cole thanks for the response. So, could you tell me how should I do it ? I dont have any idea :/

  • #69270

    Points: 0
    Rank: Member

    I used this script. It zipped file but didnt passworded :/. What is going wrong ?

    function Write-ZipUsing7Zip([string]$FilesToZip, [string]$ZipOutputFilePath, [string]$Password, [ValidateSet('7z','zip','gzip','bzip2','tar','iso','udf')][string]$CompressionType = 'zip', [switch]$HideWindow)
    # Look for the 7zip executable.
    $pathTo32Bit7Zip = "C:\Program Files (x86)\7-Zip\7z.exe"
    $pathTo64Bit7Zip = "C:\Program Files\7-Zip\7z.exe"
    $THIS_SCRIPTS_DIRECTORY = Split-Path $script:MyInvocation.MyCommand.Path
    $pathToStandAloneExe = Join-Path $THIS_SCRIPTS_DIRECTORY "7za.exe"
    if (Test-Path $pathTo64Bit7Zip) { $pathTo7ZipExe = $pathTo64Bit7Zip }
    elseif (Test-Path $pathTo32Bit7Zip) { $pathTo7ZipExe = $pathTo32Bit7Zip }
    elseif (Test-Path $pathToStandAloneExe) { $pathTo7ZipExe = $pathToStandAloneExe }
    else { throw "Could not find the 7-zip executable." }

    # Delete the destination zip file if it already exists (i.e. overwrite it).
    if (Test-Path $ZipOutputFilePath) { Remove-Item $ZipOutputFilePath -Force }

    $windowStyle = "Normal"
    if ($HideWindow) { $windowStyle = "Hidden" }

    # Create the arguments to use to zip up the files.
    # Command-line argument syntax can be found at:
    $arguments = "a -t$CompressionType ""$ZipOutputFilePath"" ""$FilesToZip"" -mx9"
    if (!([string]::IsNullOrEmpty($Password))) { $arguments += " -p$Password" }

    # Zip up the files.
    $p = Start-Process $pathTo7ZipExe -ArgumentList $arguments -Wait -PassThru -WindowStyle $windowStyle

    # If the files were not zizpped successfully.
    if (!(($p.HasExited -eq $true) -and ($p.ExitCode -eq 0)))
    throw "There was a problem creating the zip file '$ZipFilePath'."

    Write-ZipUsing7Zip -FilesToZip "D:\backupy" -ZipOutputFilePath "D:\" -Password "password123"

  • #69328

    Points: 0
    Rank: Member

    There is already module for this task in Powershell Gallery.

    Install-Module -Name 7Zip4Powershell
    Import-Module 7Zip4Powershell
    Compress-7Zip -ArchiveFileName C:\ -Path C:\Source -Format Zip -Password Passw0rd

The topic ‘Zip and passworded’ is closed to new replies.