How to Call 7-Zip CLI from PowerShell Scripts

This guide explains how to execute the 7-Zip command-line interface (CLI) from within a Microsoft PowerShell script to automate file compression and extraction. You will learn the default executable path, how to use PowerShell's call operator (&) to run commands, how to pass arguments for archiving and unzipping, and how to capture execution exit codes for error handling.

Locating 7-Zip

To use the 7-Zip CLI, you must call 7z.exe. By default, 64-bit installations place the executable in:

$sevenZipPath = "$env:ProgramFiles\7-Zip\7z.exe"

Verify that the executable exists before running commands:

if (-not (Test-Path $sevenZipPath)) {
    Throw "7-Zip executable not found at: $sevenZipPath"
}

Using the Call Operator (&)

The recommended way to execute external programs with arguments in PowerShell is the call operator (&).

1. Creating an Archive (Compression)

To compress a file or folder, use the a (add) command.

$sevenZip = "$env:ProgramFiles\7-Zip\7z.exe"
$sourcePath = "C:\Data\Logs"
$destinationZip = "C:\Backups\Logs.zip"

# Run 7-Zip to create an archive
& $sevenZip a -tzip $destinationZip $sourcePath

# Check the exit code
if ($LASTEXITCODE -eq 0) {
    Write-Output "Archive created successfully."
} else {
    Write-Error "Archiving failed with exit code $LASTEXITCODE."
}

Common switches used when creating archives:

  • -tzip or -t7z: Specifies the archive format.
  • -mx9: Sets the compression level to ultra (levels range from 0 for none to 9 for ultra).
  • -pYourPassword: Encrypts the archive with a password.

2. Extracting an Archive

To extract files, use either e (extract to current directory, ignoring folder structure) or x (extract with full paths).

Important: The -o output directory switch must not have a space between the switch and the directory path.

$sevenZip = "$env:ProgramFiles\7-Zip\7z.exe"
$archivePath = "C:\Backups\Logs.zip"
$extractPath = "C:\RestoredLogs"

# -y automatically answers yes to all prompts (overwrite files)
& $sevenZip x $archivePath "-o$extractPath" -y

if ($LASTEXITCODE -eq 0) {
    Write-Output "Extraction completed successfully."
}

Alternative Method: Using Start-Process

If you need the process to run synchronously and capture output or hide the window, use Start-Process:

$params = @{
    FilePath     = "$env:ProgramFiles\7-Zip\7z.exe"
    ArgumentList = @("a", "C:\Backups\Data.7z", "C:\Data", "-mx=9")
    NoNewWindow  = $true
    Wait         = $true
    PassThru     = $true
}

$process = Start-Process @params

if ($process.ExitCode -eq 0) {
    Write-Output "Operation completed without errors."
} else {
    Write-Error "7-Zip finished with error code: $($process.ExitCode)"
}

7-Zip Exit Codes

When handling errors, evaluate $LASTEXITCODE against standard 7-Zip return values:

  • 0: Normal execution (Success)
  • 1: Warning (Non-fatal errors, such as files locked by other processes)
  • 2: Fatal error
  • 7: Command line error (Incorrect syntax or parameters)
  • 8: Not enough memory for operation
  • 255: User stopped the process