How to Create a Password-Protected Zip File Using PowerShell?

Recently, one of my clients asked me to create an encrypted zip file. I tried PowerShell. In this PowerShell tutorial, I will show you how to create a password-protected zip file using PowerShell.

To create a password-protected zip file in PowerShell, you can use the Compress-Archive cmdlet combined with a third-party tool like 7-Zip. Here’s an example script:

$zipExePath = "C:\Program Files\7-Zip\7z.exe"
$zipFilePath = "C:\MyFolder\PasswordProtectedFile.zip"
$zipPassword = "Dummypassword"
& $zipExePath a -tzip $zipFilePath "C:\MyFolder\*" -p $zipPassword

This script assumes you have 7-Zip installed and specifies the path to its executable. It compresses the target files into a zip archive and secures it with the specified password.

Method 1: Using 7Zip4Powershell Module

One of the easiest ways to create a password-protected zip file in PowerShell is to use the 7Zip4Powershell module. This module extends the functionality of PowerShell by providing easy-to-use cmdlets for creating encrypted archives. First, you need to install the module from the PowerShell gallery.

Install-Module -Name 7Zip4Powershell

Once installed, you can compress files with a password as follows:

Compress-7Zip -ArchiveFileName 'secured.zip' -Password 'YourSecurePassword' -Path 'C:\MyFolder\*'

This command will create a zip file named ‘secured.zip’ with the specified password, including all files in the given path.

Method 2: Using 7z.exe

While PowerShell does not natively support password protection for zip files, you can still create a script that utilizes other tools or applications like 7-Zip. The script below demonstrates how to use 7-Zip with PowerShell to create a password-protected zip file.

First, ensure you have 7-Zip installed on your system. Then, you can use the following script:

$zipExePath = "C:\Program Files\7-Zip\7z.exe"
$zipFilePath = "C:\MyFolder\PasswordProtectedFile.zip"
$zipPassword = "Dummypassword"
& $zipExePath a -tzip $zipFilePath "C:\MyFolder\*" -p $zipPassword

This script sets the path to the 7-Zip executable, the files to be zipped, the output path for the zip file, and the password. It then invokes 7-Zip to create the archive.

The screenshot below shows after I executed the script using VS code in my local system. It created a zip file.

Create a Password-Protected Zip File Using PowerShell

Method 3: Using a Custom PowerShell Function

You can also write a custom function that creates a password-protected zip file. Below is an example of the complete PowerShell script.

function Create-PasswordProtectedZip {
    param(
        [string]$zipExePath,
        [string]$filesToZip,
        [string]$zipFilePath,
        [string]$zipPassword
    )

    if (Test-Path $zipExePath) {
        & $zipExePath a -tzip $zipFilePath $filesToZip -p$zipPassword
    } else {
        Write-Error "7-Zip executable not found at path: $zipExePath"
    }
}

# Usage:
Create-PasswordProtectedZip -zipExePath "C:\Program Files\7-Zip\7z.exe" `
                            -filesToZip "C:\MyFolder\*" `
                            -zipFilePath "C:\MyFolder\Output\secured.zip" `
                            -zipPassword "YourSecurePassword"

You can reuse the above function to create password-protected zip files using PowerShell.

Conclusion

Here, we got to learn how to create a password-protected zip file in PowerShell using the 7Zip4Powershell module, native cmdlets with third-party applications like 7z.exe, or write a custom function.

Do let me know if you still have any questions.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.