Zip and passworded

Hi,

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 “system.io.compression.filesystem”
[io.compression.zipfile]::CreateFromDirectory($source, $destination)

but I don’t know what to do next to passworded that file :confused:

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.

http://blog.danskingdom.com/powershell-function-to-create-a-password-protected-zip-file/

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

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: http://www.dotnetperls.com/7-zip-examples

$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:\zippedfile.zip” -Password “password123”

Hi,
There is already module for this task in Powershell Gallery. https://www.powershellgallery.com/packages/7Zip4Powershell/1.8.0

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