7-Zip command line: password-protect files, encrypt names and extract

7-Zip's command-line tool, 7z (7zz in 7-Zip's Linux build), makes a password-protected archive in one line. It's easy to get slightly wrong: without -mhe anyone can read the file names, a .zip without -mem gets the old ZipCrypto, and a password typed into the command can end up in your history. Here are the exact commands for Windows and Linux.

Short answer: run 7z a -p -mhe=on archive.7z files. -p on its own makes 7-Zip ask for the password, which keeps it out of your shell history, and -mhe=on encrypts the file names as well as the contents (.7z only). For a .zip, add -mem=AES256, because 7-Zip uses the weaker ZipCrypto by default; zip file names stay visible. Extract with 7z x archive.7z, which asks for the password.

Before you start

7-Zip is free from 7-zip.org and runs on any edition of Windows 10 or 11. The 64-bit installer puts 7z.exe in C:\Program Files\7-Zip (the 32-bit one, on 64-bit Windows, uses Program Files (x86)). If 7z is "not recognized", type "C:\Program Files\7-Zip\7z.exe" instead (in PowerShell, with & in front).

Encrypt files and file names (.7z)

Command Prompt or PowerShell
7z a -p -mhe=on Reports.7z C:\Users\you\Documents\Reports\
7z t Reports.7z
7z l Reports.7z
  1. a adds files to the archive. A folder ending in \ goes in with everything inside it.
  2. -p with nothing after it makes 7-Zip ask for the password. On Windows, what you type isn't shown. It asks only once, so test straight away.
  3. -mhe=on turns on header encryption. 7-Zip's manual says the header holds the file names, sizes and timestamps; with it on, they're encrypted along with the data. Without it, anyone can list the names with 7z l.
  4. 7z t tests the archive, asking for the password again.

The contents use AES-256, with the key derived from your password through many rounds of SHA-256. Put no space after -p: with one, 7-Zip reads the next word as a file or archive name, not as the password.

A .zip with a password: choose AES-256

Command Prompt or PowerShell
7z a -p -mem=AES256 Reports.zip C:\Users\you\Documents\Reports\

7-Zip picks the format from the extension. For .zip its default encryption is ZipCrypto. PKWARE, which publishes the ZIP specification, calls that traditional encryption "weak by today's standards", and a known-plaintext attack on it was presented in 1994. -mem=AES256 switches to AES-256, which 7-Zip's help says is supported "only by 7-Zip, WinZip and some other ZIP archivers". Microsoft says Windows 11 (version 24H2) doesn't support operations on encrypted archive files and suggests 7-Zip or WinRAR. And in any .zip the file names stay readable, because 7-Zip doesn't encrypt zip file names.

What each option can and can't do

CommandContentsFile names, sizes, datesWho can open it
7z a -p -mhe=on x.7zAES-256Encrypted7-Zip and other tools that read encrypted .7z
7z a -p x.7zAES-256VisibleSame
7z a -p -mem=AES256 x.zipAES-256Visible7-Zip, WinZip and some other zip tools
7z a -p x.zipZipCrypto (weak)VisibleMost zip tools

Passwords with special characters

The easiest fix is not to type the password into the command at all: use -p alone and type it at the prompt. If you must, quote the whole switch, and the rules depend on the shell.

Command Prompt, PowerShell, bash
C:\> 7z a "-pMy&Pass|word" Reports.7z Reports\
PS> 7z a '-pMy$Pass&word' Reports.7z Reports\
PS> 7z a '-pIt''s mine' Reports.7z Reports\
$ 7zz a '-pMy$Pass&word' Reports.7z Reports/
  • Command Prompt: Microsoft says &, | and parentheses must be preceded by ^ or put in quotation marks; inside double quotes they're taken literally. % stays special: in our test, %PATH% inside quotes became its value, and in a .bat file a lone % vanished.
  • PowerShell: single quotes pass the text exactly as typed, and '' stands for one single quote. Double quotes expand $: in our test, "-pAb$c" reached the program as -pAb.
  • bash: single quotes keep every character as it is, but a single quote can't appear inside them.
  • Double quotes in the password: on Windows, 7-Zip reads its own command line and treats every " as the start or end of a quoted part, then drops it. There's no escape: -pa"b"c silently becomes the password abc, and a single " pulls the rest of the command into the password. Use the prompt for passwords like that.

Why the password shouldn't be in the command

  • History. PowerShell's PSReadLine saves your commands to a file. It skips lines containing words such as password or secret, but 7z a -pBlue42 … contains neither. bash saves to ~/.bash_history; with HISTCONTROL=ignorespace, lines starting with a space aren't saved.
  • Other programs. While 7-Zip runs, its command line can be read: on Windows through the process's CommandLine property, on Linux in /proc/PID/cmdline, which every user can read unless the system is set up otherwise.
  • Logs. Windows can record every program's command line in the security log (event 4688). Microsoft warns that command-line arguments "can contain sensitive or private information such as passwords".
  • Scripts. A password in a .bat, .ps1 or .sh file is readable by anyone who can read the file or its backups. 7-Zip documents two ways to give a password: in -p, or at the prompt.

For unattended jobs that keep the password out of the command, see encrypting files with PowerShell and the GPG and OpenSSL guides.

Extract a password-protected archive

Command Prompt or PowerShell
7z x Reports.7z
7z x Reports.7z -oD:\Restored
7z e Reports.zip -oD:\Restored

x extracts with folder paths; e puts every file in one folder. -o sets the output folder, with no space after it. 7-Zip asks for the password when the archive needs one, and asks before overwriting unless you add -y. Exit code 0 means no error, 1 a warning, 2 a fatal error, 7 a command-line error.

On Linux: 7zz, 7z or p7zip

  • 7-Zip for Linux from 7-zip.org is a console program called 7zz (7zzs is the statically linked copy). The switches are the same; its readme says to use 7zz wherever the manual shows 7z.
  • Ubuntu 24.04: the 7zip package (sudo apt install 7zip) installs it as 7z.
  • p7zip is an older port by an independent developer. 7-Zip says it hasn't been updated since 16.02 and that version has bugs fixed in newer 7-Zip.
  • Quote wildcards so 7-Zip expands them, not the shell: 7zz a -p -mhe=on docs.7z '*.pdf'.
  • In 7-Zip's source code, the part that hides the password as you type is Windows-only, so on Linux the prompt may show what you type. Check nobody can see your screen.

More Linux options: file encryption on Linux.

What can go wrong

  • A forgotten password means lost files. 7-Zip has no reset. Keep the password in a password manager.
  • A typo at the single prompt becomes the password. That's why 7z t comes next.
  • No -mhe=on, or a .zip: names stay visible.
  • No -mem=AES256 on a .zip: you get ZipCrypto.
  • Originals left behind. 7-Zip doesn't touch them unless you add -sdel, which deletes them only after the archive is created. Test the archive before you use it.

If you'd rather encrypt each file separately with its name hidden, Clavis Encrypt does that free on Windows and Linux. Its command line, clavis-cli (Premium, $29 once; in the download from clavisenc.com, not the Microsoft Store version), never accepts a password as an argument: scripts pipe it in with --password-stdin (see encrypting files from the command line). Clavis is closed source and hasn't been independently audited; its file format is public, with an open-source decryptor.

Questions

How do I password protect a file with 7-Zip from the command line?

Run 7z a -p -mhe=on archive.7z yourfile. With -p on its own, 7-Zip asks for the password instead of taking it from the command, and -mhe=on also encrypts the file names. Then run 7z t archive.7z to check the password works.

How do I encrypt file names with 7z?

Add -mhe=on (header encryption) together with -p when you create a .7z. It encrypts file names, sizes and timestamps. It works only for .7z: 7-Zip can't encrypt file names in a .zip.

How do I extract a password-protected 7z from the command line?

Run 7z x archive.7z, and add -oFOLDER (no space) to choose where the files go. 7-Zip asks for the password. On Linux use 7zz x archive.7z, or 7z if your distribution's package installed it under that name.

How do I use a password with special characters in 7-Zip?

Best: use -p alone and type it at the prompt. Otherwise quote the whole switch: "-pMy&Pass" in Command Prompt, '-pMy$Pass' in PowerShell or bash. Avoid % in Command Prompt, and on Windows avoid double quotes in the password entirely, because 7-Zip removes them from its command line.

Can Windows open a 7-Zip AES-256 zip file?

Don't count on it. Microsoft says Windows 11 doesn't support operations on encrypted archive files and recommends 7-Zip or WinRAR.

Is ZipCrypto secure?

Not by today's standards. PKWARE, which publishes the ZIP specification, calls it weak and recommends it only for low security needs or compatibility, and a known-plaintext attack on it was presented in 1994. Use -mem=AES256 for a .zip, or a .7z.

Can 7-Zip read the password from a file or an environment variable?

7-Zip documents two ways: the password in the -p switch, or typing it when asked. Building -p from a variable still puts the password in the command line, where other programs and logs can see it.

Sources

Last checked against 7-Zip 26.06's documentation and source code and Microsoft's documentation on November 11, 2026. The quoting results are from our own tests in Command Prompt and Windows PowerShell 5.1 on Windows 11.

Related: password-protecting a zip file in Windows 11 (the 7-Zip window, step by step) and encrypting files before emailing them.

Get Clavis Premium — $29 once Try the free installer first

One-time payment, tax included, no subscription. The command line comes with the installer from clavisenc.com, not the Microsoft Store version, so start with the free installer.