PowerShell: Zip up a folder and email it
Posted
Wednesday, November 29, 2006 11:46 AM
by
mhodnick
Here's a quick little PowerShell script you can use in conjunction with Out-Zip to zip up a folder on your hard drive and email it:
$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)