Home > PowerShell > PowerShell function to create a password protected zip file

PowerShell function to create a password protected zip file

There are a few different ways to create zip files in powershell, but not many that allow you to create one that is password protected.  I found this post that shows how to do it using 7zip, so I thought I would share my modified solution.

Here is the function I wrote that uses 7zip to perform the zip, since 7zip supports using a password to zip the files.  This script looks for the 7zip executable (7z.exe) in the default install locations, and if not found it will use the stand-alone 7zip executable (7za.exe) if it is in the same directory as the powershell script.

Updated function to support multiple compression types: 7z, zip, gzip, bzip2, tar, iso, and udf.

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 zipped successfully.
	if (!(($p.HasExited -eq $true) -and ($p.ExitCode -eq 0))) 
		throw "There was a problem creating the zip file '$ZipFilePath'."

And here’s some examples of how to call the function:

Write-ZipUsing7Zip -FilesToZip "C:\SomeFolder" -ZipOutputFilePath "C:\SomeFolder.zip" -Password "password123"
Write-ZipUsing7Zip "C:\Folder\*.txt" "C:\FoldersTxtFiles.zip" -HideWindow


I hope you find this useful.

Happy coding!

  1. May 12th, 2013 at 16:31 | #1

    Nice function, but for a little more security I’d look into using the -t7z option along with a password. The regular zip format with a password isn’t very strong protection any more, there are many programs for cracking this out there.


    This link covers some of the essentials to get encrypted files with 7zip


    • May 13th, 2013 at 10:18 | #2

      Thanks for the comment. I chose to implement it as zip purely for compatibility reasons, as Windows can now handle .zip files natively, and not everybody knows what a .7z file is. I’ve updated the function to take the compression type as a parameter, so it’s easy to specify to make a 7z file now; zip is still the default though. Thanks 🙂

  2. SkyBuck
    November 21st, 2014 at 13:33 | #3

    How it’s possible to compress from a UNC path ? I need to compress a folder from my NAS.

  1. July 23rd, 2013 at 08:39 | #1
  2. July 26th, 2013 at 05:21 | #2

%d bloggers like this: