PowerShell Script: Zip up a folder and email it

Cool method for scripting attaching a file and emailing using Powershell.  The script also displays how to zip up a destination using Out-zip.

$sender = sender@host.com
$recipient = recipient@host.com
$server = mail.host.com
$targetFolder = c:\MyFolder
$file = c:\MyZipFile.zip
if ( [System.IO.File]::Exists($file) )
{
  remove-item -force $file
}

gi $targetFolder | out-zip $file $_
$subject = “Sending a File ” + [System.DateTime]::Now
$body = “I’m sending a file!”
$msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
$attachment = new-object System.Net.Mail.Attachment $file
$msg.Attachments.Add($attachment)
$client = new-object System.Net.Mail.SmtpClient $server
$client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$client.Send($msg)

Mike Hodnick : PowerShell: Zip up a folder and email it

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • Simpy

0 Responses to “PowerShell Script: Zip up a folder and email it”


  1. No Comments