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 $zipPasswordThis 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 7Zip4PowershellOnce 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 $zipPasswordThis 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.

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:
- How to Get Details of a File in PowerShell?
- Count Files in a Folder Using PowerShell
- Rename Files with Date in PowerShell
- Encrypt a File with a Password in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.