{"id":7331,"date":"2015-03-12T00:01:00","date_gmt":"2015-03-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/12\/use-powershell-to-create-archive-and-send-email\/"},"modified":"2019-02-18T10:30:19","modified_gmt":"2019-02-18T17:30:19","slug":"use-powershell-to-create-archive-and-send-email","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-archive-and-send-email\/","title":{"rendered":"Use PowerShell to Create Archive and Send Email"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create a .zip archive and email it.<\/span>\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy! I have a number of files that I would like to archive on a regular basis to a .zip file. I need to email that .zip file as an attachment. I do this every week, and it takes me like nearly 15 minutes to create the .zip file, open Outlook, squirrel around and find the file, write the email, send the thing, and then delete the .zip archive from disk. If I could run a script, it would save me over an hour a month&mdash;that is nearly two days of free labor. Can you do it?\n&mdash;JB\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello JB,\nMicrosoft Scripting Guy, Ed Wilson, is here. Today I am sipping a nice cup of Earl Grey tea with a cinnamon stick in it. I was talking to my old high-school friend last night. He is heading to Raleigh to see a concert, and he thought it would be cool to get together on his way through Charlotte. Unfortunately, we will be on the coast when he comes floating by. Oh well. Hopefully, we will get together again soon. It is nice to keep up with friends, even when they don&rsquo;t provide much heads-up before they want to stop by.\nThe cool thing about using Windows PowerShell to automate creating .zip archives and emailing them is that it is easily scripted. In fact, it can easily be turned into a scheduled task if one wishes to take the next logical step.\nThe first thing to do is to create a .zip archive of the folder that contains the files that you want to archive. I like to create two variables: one for the source files and one for the destination that will hold the .zip archive of the files. I then use the <b>Test-Path<\/b> cmdlet to ensure the archive file is not already there, and if it is, I delete it. This prevents error messages. Here is the first three lines of my script:<\/p>\n<p style=\"margin-left:30px\">$source = &#8220;C:fso&#8221;<\/p>\n<p style=\"margin-left:30px\">$destination = &#8220;C:backupFSO_Backup.zip&#8221;<\/p>\n<p style=\"margin-left:30px\">&nbsp;If(Test-path $destination) {Remove-item $destination}\nNow I need to add the assembly that contains the compression classes from .NET Framework 4.5. To do this, I use the <b>Add-Type<\/b> cmdlet, specify that it is an assembly, and provide the name System.IO.Compression.Filesystem to it. This command is shown here:<\/p>\n<p style=\"margin-left:30px\">Add-Type -assembly &#8220;system.io.compression.filesystem&#8221;\nThe final command creates the .zip archive. It uses the <b>CreateFromDirectory<\/b> static method from the <b>ZipFile<\/b> class:<\/p>\n<p style=\"margin-left:30px\">[io.compression.zipfile]::CreateFromDirectory($Source, $destination)&nbsp;\nThis section of the script is shown here:<\/p>\n<p style=\"margin-left:30px\">$source = &#8220;C:fso&#8221;<\/p>\n<p style=\"margin-left:30px\">$destination = &#8220;C:backupFSO_Backup.zip&#8221;<\/p>\n<p style=\"margin-left:30px\">&nbsp;If(Test-path $destination) {Remove-item $destination}<\/p>\n<p style=\"margin-left:30px\">Add-Type -assembly &#8220;system.io.compression.filesystem&#8221;<\/p>\n<p style=\"margin-left:30px\">[io.compression.zipfile]::CreateFromDirectory($Source, $destination)&nbsp;\nNow I need to email the archive. To do this, I use the <b>Send-MailMessage<\/b> cmdlet. This cmdlet can do all sorts of stuff, but for our purposes, I need to add the <b>To<\/b> and <b>From<\/b> parameters, the attachment path, the subject, and the body message.\nThis is all pretty simple. I also need to specify the appropriate SMTP server for my mail application, and specify if I need to use credentials or SSL. It may take a bit of practice or research to get everything working perfectly. But when it is done, well, it is done. Here is the command I use:<\/p>\n<p style=\"margin-left:30px\">Send-MailMessage -From &#8220;ScriptingGuys@Outlook.com&#8221; -To &#8220;ScriptingGuys@Outlook.com&#8221; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;-Attachments $destination -Subject &#8220;$(Split-Path $destination -Leaf)&#8221; -Body &#8220;File attached&#8221; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;-SmtpServer &#8220;smtp-mail.outlook.com&#8221; -UseSsl -Credential &#8220;ScriptingGUys@Outlook.com&#8221;\nThe last requirement is to clean up by deleting the previously created .zip archive. This is simple. I use a <b>Remove-Item<\/b> command that uses the <b>$Destination<\/b> variable:<\/p>\n<p style=\"margin-left:30px\">Remove-Item $destination\nThe complete script is shown here:<\/p>\n<p style=\"margin-left:30px\">$source = &#8220;C:fso&#8221;<\/p>\n<p style=\"margin-left:30px\">$destination = &#8220;C:backupFSO_Backup.zip&#8221;<\/p>\n<p style=\"margin-left:30px\">&nbsp;If(Test-path $destination) {Remove-item $destination}<\/p>\n<p style=\"margin-left:30px\">Add-Type -assembly &#8220;system.io.compression.filesystem&#8221;<\/p>\n<p style=\"margin-left:30px\">[io.compression.zipfile]::CreateFromDirectory($Source, $destination)<\/p>\n<p style=\"margin-left:30px\">Send-MailMessage -From &#8220;ScriptingGuys@Outlook.com&#8221; -To &#8220;ScriptingGuys@Outlook.com&#8221; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;-Attachments $destination -Subject &#8220;$(Split-Path $destination -Leaf)&#8221; -Body &#8220;File attached&#8221; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;-SmtpServer &#8220;smtp-mail.outlook.com&#8221; -UseSsl -Credential &#8220;ScriptingGUys@Outlook.com&#8221;<\/p>\n<p style=\"margin-left:30px\">&nbsp;Remove-Item $destination\nJB, that is all there is to using Windows PowerShell to create a .zip archive and to email it off.&nbsp; Join me tomorrow when I will talk about more cool Windows PowerShell stuff.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create a .zip archive and email it. &nbsp;Hey, Scripting Guy! I have a number of files that I would like to archive on a regular basis to a .zip file. I need to email that .zip file as an attachment. I do [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[567,3,4,574,12,45],"class_list":["post-7331","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files-and-folders","tag-scripting-guy","tag-scripting-techniques","tag-sending-mail","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create a .zip archive and email it. &nbsp;Hey, Scripting Guy! I have a number of files that I would like to archive on a regular basis to a .zip file. I need to email that .zip file as an attachment. I do [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=7331"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7331\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=7331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}