7-Zip CLI Return Codes: Exit Codes Explained

When running 7-Zip from the command line, the utility returns specific numeric exit codes (also called error levels or return codes) to indicate whether an operation completed successfully, encountered minor issues, or failed completely. This guide provides a direct breakdown of all official 7-Zip command-line interface (CLI) return codes so you can accurately handle success, warnings, and errors in your automated scripts and batch files.

7-Zip Exit Codes List

7-Zip uses standard process exit codes to communicate the result of an archive creation, extraction, or testing process:

  • 0 – Success (No error)
    The operation completed successfully without any issues. All specified files were processed, compressed, or extracted as intended.

  • 1 – Warning (Non-fatal error)
    The operation completed, but minor issues occurred. This most commonly happens when 7-Zip cannot access one or more files because they are locked by another program. The remaining files are still processed, but the archive may be incomplete.

  • 2 – Fatal error
    A critical error occurred that prevented the operation from completing. Typical causes include a corrupted archive, incorrect passwords, invalid archive headers, or missing output destinations.

  • 7 – Command line error
    The command syntax was incorrect. This occurs if you supply invalid command switches, misplace arguments, or use unsupported options.

  • 8 – Not enough memory
    The system ran out of RAM required to perform the compression, decompression, or dictionary allocation needed for the operation.

  • 255 – User stopped the process
    The operation was manually terminated by the user before completion (such as pressing Ctrl + C in the console).

How to Check 7-Zip Return Codes in Scripts

You can inspect these codes immediately after running a 7-Zip command to handle errors programmatically.

Windows Command Prompt / Batch

In Windows batch scripts, capture the result using the %ERRORLEVEL% variable:

7z a backup.7z C:\Data\*
if %ERRORLEVEL% EQU 0 (
    echo Backup completed successfully.
) else if %ERRORLEVEL% EQU 1 (
    echo Backup finished with warnings.
) else (
    echo Backup failed with error code %ERRORLEVEL%.
)

PowerShell

In PowerShell, use the $LASTEXITCODE variable:

& 7z a backup.7z C:\Data\*
if ($LASTEXITCODE -eq 0) {
    Write-Host "Success"
} else {
    Write-Host "7-Zip failed with code $LASTEXITCODE"
}

Linux / Bash

In Unix-like environments using p7zip, check the $? variable:

7z a backup.7z /home/user/data/*
STATUS=$?
if [ $STATUS -eq 0 ]; then
    echo "Success"
elif [ $STATUS -eq 1 ]; then
    echo "Warning: Some files could not be read"
else
    echo "Error: Failed with code $STATUS"
fi