PowerShell Script to Test 7-Zip Archive Integrity

This article demonstrates how to automate integrity testing for all 7-Zip archive files within a specific folder using a PowerShell script. By leveraging the command-line utility provided with 7-Zip (7z.exe), you can quickly scan directories, verify that your archives are free from corruption, and log any errors found.

Prerequisites

To run the verification process, ensure that 7-Zip is installed on your Windows machine. The standard installation path is typically C:\Program Files\7-Zip\7z.exe.

The PowerShell Script

Save the following script as a .ps1 file (for example, Test-Archives.ps1) or run it directly inside the PowerShell console:

# Define the path to 7z.exe
$7zipPath = "C:\Program Files\7-Zip\7z.exe"

# Define the directory containing the archives
$targetFolder = "C:\Path\To\Your\Archives"

# Check if 7-Zip executable exists
if (-not (Test-Path -Path $7zipPath)) {
    Write-Error "7-Zip executable not found at: $7zipPath"
    exit 1
}

# Supported archive extensions
$extensions = @("*.7z", "*.zip", "*.rar", "*.tar", "*.gz")

# Retrieve all archives in the target folder (include -Recurse if checking subfolders)
$archives = Get-ChildItem -Path $targetFolder -Include $extensions -File

if ($archives.Count -eq 0) {
    Write-Host "No archives found in $targetFolder" -ForegroundColor Yellow
    exit 0
}

Write-Host "Found $($archives.Count) archive(s). Beginning integrity checks..." -ForegroundColor Cyan

# Iterate through each archive and test integrity
foreach ($archive in $archives) {
    Write-Host "Testing: $($archive.Name)..." -NoNewline

    # Run the 7-Zip 't' (test) command quietly
    $process = Start-Process -FilePath $7zipPath -ArgumentList "t `"$($archive.FullName)`" -bso0 -bse0" -Wait -PassThru -NoNewWindow

    # 7-Zip returns exit code 0 on success
    if ($process.ExitCode -eq 0) {
        Write-Host " [PASSED]" -ForegroundColor Green
    } else {
        Write-Host " [FAILED] (Exit Code: $($process.ExitCode))" -ForegroundColor Red
    }
}

Write-Host "Integrity testing complete." -ForegroundColor Cyan

How It Works

  • 7z.exe t: The t command tells 7-Zip to test the integrity of the archive by decompressing the data in memory and validating checksums without writing files to disk.
  • -bso0 -bse0: These parameters suppress standard output and standard error messages to keep the PowerShell console output clean and readable.
  • Start-Process -PassThru -Wait: Executes the process synchronously and captures the exit code once complete.
  • ExitCode 0: In 7-Zip, an exit code of 0 indicates that the operation finished with no errors. Any non-zero exit code (such as 1 for warning or 2 for fatal error) indicates a corrupted archive, incorrect password, or unreadable file.