Send Email Notifications After 7-Zip Backups

Automating email notifications following a 7-Zip backup ensures you are immediately alerted to the success or failure of your data protection routines. This guide demonstrates how to create a complete automation script using PowerShell, capture 7-Zip exit codes to determine the backup status, send an email alert via SMTP, and schedule the entire process using Windows Task Scheduler.

1. Understanding 7-Zip Exit Codes

When 7-Zip executes via command-line (7z.exe), it returns an exit code indicating the outcome:

  • 0: Success (no errors).
  • 1: Warning (non-fatal errors, such as locked files that were skipped).
  • 2: Fatal error.
  • 7: Command-line error.
  • 8: Not enough memory for operation.
  • 255: Process was stopped by the user.

A robust script checks this exit code to tailor the email subject and body accordingly.


2. The PowerShell Automation Script

PowerShell provides native support for executing processes and sending emails via SMTP. Create a file named backup_with_notification.ps1 and paste the following script:

# ==============================
# Configuration
# ==============================
$7zipPath    = "C:\Program Files\7-Zip\7z.exe"
$SourcePath  = "C:\Data\ImportantFiles"
$Destination = "D:\Backups\Backup_$(Get-Date -Format 'yyyyMMdd_HHmmss').7z"
$LogFile     = "C:\Logs\backup_$(Get-Date -Format 'yyyyMMdd').log"

# SMTP Settings
$SmtpServer  = "smtp.yourprovider.com"
$SmtpPort    = 587
$SmtpUser    = "alerts@yourdomain.com"
$SmtpPass    = "YourPasswordHere" | ConvertTo-SecureString -AsPlainText -Force
$From        = "alerts@yourdomain.com"
$To          = "admin@yourdomain.com"

# ==============================
# Execute 7-Zip Backup
# ==============================
$Arguments = "a -t7z `"$Destination`" `"$SourcePath`" -mx=5"

# Run 7-Zip and redirect output to the log file
Start-Process -FilePath $7zipPath -ArgumentList $Arguments -Wait -NoNewWindow -RedirectStandardOutput $LogFile -PassThru | Out-Null
$ExitCode = $LASTEXITCODE

# ==============================
# Evaluate Results and Set Message
# ==============================
if ($ExitCode -eq 0) {
    $Subject = "SUCCESS: 7-Zip Backup Completed"
    $Body = "The backup completed successfully without errors.`n`nDestination: $Destination"
} elseif ($ExitCode -eq 1) {
    $Subject = "WARNING: 7-Zip Backup Completed with Warnings"
    $Body = "The backup finished, but some files could not be processed.`nCheck log file at: $LogFile"
} else {
    $Subject = "FAILURE: 7-Zip Backup Failed (Exit Code: $ExitCode)"
    $Body = "The backup task failed.`nExit Code: $ExitCode`nCheck log file at: $LogFile"
}

# ==============================
# Send Email Notification
# ==============================
$Credential = New-Object System.Management.Automation.PSCredential ($SmtpUser, $SmtpPass)

$MailParams = @{
    SmtpServer  = $SmtpServer
    Port        = $SmtpPort
    UseSsl      = $true
    Credential  = $Credential
    From        = $From
    To          = $To
    Subject     = $Subject
    Body        = $Body
    Attachments = $LogFile
}

Send-MailMessage @MailParams

3. Testing the Script

Before scheduling, verify the script manually:

  1. Open PowerShell as Administrator.
  2. Run the script:
    & "C:\Scripts\backup_with_notification.ps1"
  3. Check your recipient inbox for the email alert and verify that the archive file has been generated.

4. Scheduling the Task in Windows

To run the backup and email process automatically, configure Windows Task Scheduler:

  1. Open Task Scheduler from the Start Menu.
  2. Select Create Task in the right-hand panel.
  3. On the General tab:
    • Name the task (e.g., Daily 7-Zip Backup).
    • Select Run whether user is logged on or not.
    • Check Run with highest privileges.
  4. On the Triggers tab:
    • Click New and set your desired frequency (e.g., Daily at 2:00 AM).
  5. On the Actions tab:
    • Click New.
    • Set Action to Start a program.
    • In Program/script, enter: powershell.exe
    • In Add arguments (optional), enter:
      -ExecutionPolicy Bypass -File "C:\Scripts\backup_with_notification.ps1"
  6. Click OK and enter your Windows credentials when prompted to save the task.