Skip to content

Instantly share code, notes, and snippets.

@smasterson
Created February 21, 2014 18:55
Show Gist options
  • Save smasterson/9140810 to your computer and use it in GitHub Desktop.
Save smasterson/9140810 to your computer and use it in GitHub Desktop.
# Simple Backup to Email script
# Requirements: 7-Zip to be installed
# User Variables
# Folder to zip and send
$TheFolder = "E:\ZipMe"
# (sub)Folder to exclude
$fExclude = "SkipMe"
# Path to zip file (output)
$theDay = Get-Date -Format "MMddyyyy"
$theTime = Get-Date -Format "HHmmss"
$outfile = "E:\zippedfolder_$theDay-$theTime.bak"
# Path to 7-zip
$7z = "E:\Program Files\7-Zip\7z.exe"
# 7-zip Arguments
$7zArgs = "a", "$outfile", "$TheFolder", "-xr!$fExclude"
# Email setup
$emailsubject = "Archive - " + $TheFolder + " - Rename file extension to .7z"
$sendTo = "youraddy@whoever.com"
$sendFrom = "sender@whoever.com"
$smtpserver = "your.relay.com"
# End User Variables
# Zip it up and save 7z output as email msg body
[string]$Body = & $7z $7zArgs
# Send output to email
send-mailmessage -to $sendTo -from $sendFrom -Subject $emailsubject -smtpserver $smtpserver -Body $Body -Attachments $outfile
#Remove zip file
Remove-Item $outfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment