Using 7-Zip via the command line allows you to automate archive creation, extract files in bulk, and integrate compression tasks into batch scripts without using the graphical user interface. To achieve this, you must use the 7z.exe executable located in the 7-Zip installation folder. The basic syntax follows the pattern: 7z .
What You'll Need
- A Windows PC with 7-Zip installed.
- Administrative privileges (required only for adding 7-Zip to the system PATH environment variable).
- Command Prompt (cmd.exe) or PowerShell.
- Knowledge of the file paths for the folders you wish to compress or extract.
Step-by-Step
- Locate the 7-Zip Executable: By default, 7-Zip installs to
C:\Program Files\7-Zip\7z.exe. You can run the program by navigating to this folder in the command prompt or by providing the full path in your command. - Add 7-Zip to System PATH (Optional but Recommended): To avoid typing the full path every time, add the installation folder to your Windows Environment Variables. Search for "Edit the system environment variables" in the Start menu, click "Environment Variables," find the "Path" variable under System Variables, click "Edit," and add
C:\Program Files\7-Zip\to the list. Restart your command prompt for this to take effect. - Create a Basic Archive: To compress a folder or a file into a .7z archive, use the
a(add) command. For example, to compress a folder named "Documents" into an archive called "Backup.7z", type:7z a Backup.7z C:\Users\Name\Documents
This command tells 7-Zip to add the specified source to the target archive. - Extract Files to the Current Directory: To extract the contents of an archive into the folder you are currently in, use the
e(extract) command:7z e Backup.7z
Note that theecommand extracts all files into a single flat folder, ignoring any internal folder structure within the archive. - Extract Files with Full Paths: To maintain the original folder hierarchy of the archive, use the
x(extract with full paths) command:7z x Backup.7z
This is generally the preferred method for extracting backups or software packages to ensure files remain in their intended directories. - Create a Password-Protected Archive: To secure your files, use the
-pswitch followed immediately by your password. For example:7z a Secure.7z C:\PrivateFolder -pMySecretPassword123
If you leave the password blank (-p), 7-Zip will prompt you to enter the password manually in the console for better security. - Encrypt File Names: By default, passwords protect the contents of the files, but the filenames are still visible. To encrypt the filenames themselves (only available for .7z format), add the
-mhe=onswitch:7z a Secure.7z C:\PrivateFolder -pMySecretPassword123 -mhe=on - Compress to ZIP Format: While .7z is the default, you can create standard .zip files for better compatibility with other operating systems by specifying the extension:
7z a Archive.zip C:\MyFolder - Update Existing Archives: To add only new or changed files to an existing archive without re-compressing everything, use the
u(update) command:7z u Backup.7z C:\Users\Name\Documents - List Contents of an Archive: Before extracting, you can view what is inside an archive using the
l(list) command:7z l Backup.7z
Common Mistakes
One of the most frequent errors is confusing the e and x commands. Using e on a complex project folder will dump every single file from every subfolder into one giant list in the root directory, which often leads to filename collisions and a disorganized mess. Always use x unless you specifically want a flat file list.
Another common mistake is forgetting to wrap file paths in double quotes. If your folder path contains spaces (e.g., C:\My Documents\Work Files), the command line will interpret the space as the end of the argument, resulting in a "System ERROR: Cannot open file" message. Always use quotes: 7z a "Backup.7z" "C:\My Documents\Work Files".
Users also frequently mistake the -p switch for a separate command. The switches must come after the main command (like a or x) and before the file names, or immediately following them, but they must be prefixed with a hyphen. Additionally, there should be no space between -p and the actual password if you are typing it in plain text.
Tips
For maximum compression, you can use the -mx switch to set the compression level. The scale ranges from 0 (Store/No compression) to 9 (Ultra). For example, 7z a Archive.7z Folder -mx9 will produce the smallest possible file size but will take more time and CPU power to process.
If you are automating backups via a .bat file, you can use the -y switch. This tells 7-Zip to assume "Yes" to all prompts, such as whether to overwrite an existing file. This prevents your script from hanging while waiting for user input.
To exclude specific files or folders from an archive, use the -x switch (note that this is different from the x extract command). For instance, to exclude all .log files from a backup, use 7z a Backup.7z C:\MyFolder -x!*.log.
When dealing with very large archives, you can split them into smaller volumes (chunks) using the -v switch. For example, 7z a Archive.7z C:\LargeFolder -v100m will create multiple files of 100 megabytes each.
Troubleshooting
"'7z' is not recognized as an internal or external command"
This occurs because the 7-Zip folder is not in your system's PATH environment variable. You have two options: either add the folder to the PATH as described in the step-by-step section, or use the full path to the executable in every command (e.g., "C:\Program Files\7-Zip\7z.exe" a archive.7z folder).
"System ERROR: Cannot open file as archive"
This usually means the file you are trying to extract is corrupted, is not actually a supported archive format, or you have a typo in the filename. Verify the file extension and ensure the file exists in the directory you are currently targeting.
"Access is denied"
This happens when 7-Zip tries to write a file to a protected directory (like C:\Windows or C:\Program Files) without sufficient permissions. To fix this, right-click your Command Prompt or PowerShell icon and select "Run as administrator."
Extraction stops unexpectedly
Check your available disk space. 7-Zip requires temporary space to unpack files. If your drive is nearly full, the process will fail mid-way. Clear space or specify a different temporary directory using the -t switch if necessary.
Frequently Asked Questions
Can I use 7-Zip command line to create RAR files?
No. 7-Zip can extract RAR files, but it cannot create them because the RAR format is proprietary. To create RAR files, you must use WinRAR.
How do I remove a single file from an existing archive?
Use the d (delete) command. For example, 7z d Archive.7z file_to_remove.txt will remove that specific file from the archive.
Is the command line version faster than the GUI?
The compression speed is the same because both use the same engine. However, the command line is "faster" for the user when performing repetitive tasks or processing hundreds of folders via a script.
Can I compress multiple folders into one archive?
Yes. Simply list all the folders or files you want to include after the archive name: 7z a Combined.7z Folder1 Folder2 File1.txt.
How do I check the version of 7-Zip I am using?
Run the command 7z without any arguments. The first few lines of the output will display the version number and the copyright information.