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.
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)
7z a -p -mhe=on Reports.7z C:\Users\you\Documents\Reports\
7z t Reports.7z
7z l Reports.7zaadds files to the archive. A folder ending in\goes in with everything inside it.-pwith 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.-mhe=onturns 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 with7z l.7z ttests 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
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
| Command | Contents | File names, sizes, dates | Who can open it |
|---|---|---|---|
7z a -p -mhe=on x.7z | AES-256 | Encrypted | 7-Zip and other tools that read encrypted .7z |
7z a -p x.7z | AES-256 | Visible | Same |
7z a -p -mem=AES256 x.zip | AES-256 | Visible | 7-Zip, WinZip and some other zip tools |
7z a -p x.zip | ZipCrypto (weak) | Visible | Most 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.
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"csilently becomes the passwordabc, 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; withHISTCONTROL=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
7z x Reports.7z
7z x Reports.7z -oD:\Restored
7z e Reports.zip -oD:\Restoredx 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(7zzsis the statically linked copy). The switches are the same; its readme says to use7zzwherever the manual shows7z. - Ubuntu 24.04: the
7zippackage (sudo apt install 7zip) installs it as7z. - 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 tcomes next. - No
-mhe=on, or a .zip: names stay visible. - No
-mem=AES256on 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
- 7-Zip home page (AES-256 in 7z and ZIP; Windows 11 and 10)
- 7-Zip: 7z format (AES-256; key derived from the password with many SHA-256 iterations)
- 7-Zip downloads (7-Zip for Linux console version; its readme names 7zz and 7zzs, and the download includes the current manual)
- 7-Zip links (p7zip not updated since 16.02, with bugs fixed in newer 7-Zip)
- 7-Zip help: -p switch (online copy of the help that ships with 7-Zip; the manual in the current download adds that -p without a password makes 7-Zip ask for one and that zip file names can't be encrypted)
- 7-Zip help: -m switch (he header encryption for 7z; em encryption method for zip, default ZipCrypto; the manual in the current download adds that the header holds file names, sizes and timestamps)
- 7-Zip source: 7zipInstall.c (the installer's default folder: Program Files\7-Zip, from Windows' ProgramFilesDir setting)
- 7-Zip help: command line syntax (archive name, quoting, wildcards)
- 7-Zip help: -t switch (archive type from the file extension)
- 7-Zip help: x command (extract with full paths)
- 7-Zip help: e command (all files into one folder; asks before overwriting unless -y)
- 7-Zip help: -o switch (output folder)
- 7-Zip help: -sdel switch (delete files after the archive is created)
- 7-Zip help: exit codes
- 7-Zip help: Add to Archive dialog (ZipCrypto for compatibility; AES-256 supported by 7-Zip, WinZip and some others)
- 7-Zip source: CommandLineParser.cpp (on Windows, double quotes toggle quoting and are removed)
- 7-Zip source: UserInputUtils.cpp (password prompt; typing is hidden on Windows only)
- 7-Zip source: UpdateCallbackConsole.cpp (asks for the password once when creating an archive)
- PKWARE: .ZIP File Format Specification, APPNOTE 6.3.10, section 6.0 (traditional encryption is weak by today's standards)
- Biham and Kocher: A known plaintext attack on the PKZIP stream cipher (Fast Software Encryption 1994)
- Microsoft Support: Zip and unzip files (Windows 11 doesn't support operations on encrypted archives)
- Microsoft Learn: cmd (special characters need ^ or quotation marks; %variable% substitution)
- Microsoft Learn: about_Quoting_Rules (single-quoted strings are verbatim; '' for a single quote)
- Microsoft Learn: about_PSReadLine (history file and its sensitive-word filter)
- Microsoft Learn: Win32_Process (CommandLine property)
- Microsoft Learn: Command line process auditing (event 4688; arguments can contain passwords)
- bash(1) manual (single quotes, HISTCONTROL, ~/.bash_history)
- proc_pid_cmdline(5) (a process's complete command line)
- proc(5) (hidepid: by default everybody may access all /proc/PID directories)
- Ubuntu packages: 7zip in Ubuntu 24.04 (installs /usr/bin/7z)
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.
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.