Use PowerShell to Create Archive and Send Email


Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create a .zip archive and email it.

Hey, Scripting Guy! Question 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—that is nearly two days of free labor. Can you do it?

—JB

Hey, Scripting Guy! Answer Hello JB,

Microsoft 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’t provide much heads-up before they want to stop by.

The 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.

The 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 Test-Path 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:

$source = "C:fso"

$destination = "C:backupFSO_Backup.zip"

 If(Test-path $destination) {Remove-item $destination}

Now I need to add the assembly that contains the compression classes from .NET Framework 4.5. To do this, I use the Add-Type cmdlet, specify that it is an assembly, and provide the name System.IO.Compression.Filesystem to it. This command is shown here:

Add-Type -assembly "system.io.compression.filesystem"

The final command creates the .zip archive. It uses the CreateFromDirectory static method from the ZipFile class:

[io.compression.zipfile]::CreateFromDirectory($Source, $destination) 

This section of the script is shown here:

$source = "C:fso"

$destination = "C:backupFSO_Backup.zip"

 If(Test-path $destination) {Remove-item $destination}

Add-Type -assembly "system.io.compression.filesystem"

[io.compression.zipfile]::CreateFromDirectory($Source, $destination) 

Now I need to email the archive. To do this, I use the Send-MailMessage cmdlet. This cmdlet can do all sorts of stuff, but for our purposes, I need to add the To and From parameters, the attachment path, the subject, and the body message.

This 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:

Send-MailMessage -From "ScriptingGuys@Outlook.com" -To "ScriptingGuys@Outlook.com" `

 -Attachments $destination -Subject "$(Split-Path $destination -Leaf)" -Body "File attached" `

 -SmtpServer "smtp-mail.outlook.com" -UseSsl -Credential "ScriptingGUys@Outlook.com"

The last requirement is to clean up by deleting the previously created .zip archive. This is simple. I use a Remove-Item command that uses the $Destination variable:

Remove-Item $destination

The complete script is shown here:

$source = "C:fso"

$destination = "C:backupFSO_Backup.zip"

 If(Test-path $destination) {Remove-item $destination}

Add-Type -assembly "system.io.compression.filesystem"

[io.compression.zipfile]::CreateFromDirectory($Source, $destination)

Send-MailMessage -From "ScriptingGuys@Outlook.com" -To "ScriptingGuys@Outlook.com" `

 -Attachments $destination -Subject "$(Split-Path $destination -Leaf)" -Body "File attached" `

 -SmtpServer "smtp-mail.outlook.com" -UseSsl -Credential "ScriptingGUys@Outlook.com"

 Remove-Item $destination

JB, that is all there is to using Windows PowerShell to create a .zip archive and to email it off.  Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (2)

  1. Steve says:

    Can we get an EWS powershell example of sending so that Sent Items are saved?

  2. harshal says:

    Hello sir,
    I am new to Powershell script.I wants to unzip a file(containing folder and files) from url and extract it to specific directory and again if run the same script again,then it should overwrite the content of the directory without prompt for overwrite.
    I used Following Tryout: Add-Type -assembly "system.io.compression.filesystem
    1) [io.compression.zipfile]::ExtractToDirectory(($source,$destination),True)
    2) [io.compression.zipfile]::ExtractToDirectory("c:tempEclipse.zip", "c:",$Force.IsPresent)

    BUT It doesnot works. Please Help For This…

Skip to main content