Software

Send an email with an attachment using PowerShell

Sending an email with PowerShell can be a great way to deliver diagnostic data under the correct circumstances.

Sending an email with PowerShell can be a great way to deliver data under the correct circumstances. It takes some additional work compared to exporting a file, but the process can be simple with the proper reference.

From the console

Often, PowerShell will give you the results you need in the console window only, or you can specify an output file or set-content location to store the results of a script. While this often is sufficient, sending an email with your PowerShell script can be a great way to deliver data.

There are a few properties and variables you'll want to define — the first is the IP address or hostname of the server you will want the e-mail relayed through. If you choose to run the script from the e-mail server, for example, you can use the localhost address.

$smtpServer = "127.0.0.1"

We will also reference the file we wish to attach — this may have been created earlier in the script, it may be a static filename that's replaced on a regular basis, or it may be dynamic based on your needs.

$file = "C:\folder\file.csv"
$att = new-object Net.Mail.Attachment($file)

Then, we'll import the necessary .NET objects.

$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

Now we'll fill in the other properties of the email. The "from" address can be of particular importance based on your organization's email filtering and spam policies. You can use variables to fill in any of the below values.

$msg.From = "emailadmin@test.com"

You can add as many of the following lines as you want to add multiple recipients to the email.

$msg.To.Add("user1@test.com")
$msg.To.Add("user2@test.com")

Set the subject of the email with this line.

$msg.Subject = "Notification from email server"

Use the @" to open and "@ to close a multi-line string. It's likely that the body of the e-mail will be multiple lines, though if it isn't, you can use regular double quotes.

$msg.Body = @"This is an example

message

body"@

Now, attach the file defined above.

$msg.Attachments.Add($att)

The following two lines will send the message and then remove the attachment from memory. Failing to dispose the attachment will result in unnecessary use of the system's memory, which may eventually impact system performance.

$smtp.Send($msg)
$att.Dispose()

Example

Here's an example of a use for this functionality in a script for an Exchange 2007/2010 Server. The purpose of this script is to create a .csv report of mailbox statistics and email it to administrators. If you plan to use the script below, make sure you replace the example values provided with your own legitimate information.

Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue

$file = "C:\folder\file.csv"

$mailboxdata = (Get-MailboxStatistics | select DisplayName, TotalItemSize,TotalDeletedItemSize, ItemCount, LastLoggedOnUserAccount, LastLogonTime)

$mailboxdata | export-csv "$file"

$smtpServer = "127.0.0.1"

$att = new-object Net.Mail.Attachment($file)

$msg = new-object Net.Mail.MailMessage

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = "emailadmin@test.com"

$msg.To.Add("administrator1@test.com")

$msg.To.Add("administrator2@test.com")

$msg.Subject = "Notification from email server"

$msg.Body = "Attached is the email server mailbox report"

$msg.Attachments.Add($att)

$smtp.Send($msg)

$att.Dispose()

About Harold Ogden

Harold Ogden is a Systems and Security Architect in the higher education industry. He has experience in both technical and managerial roles, and strives to bring common sense to the worlds of Information Technology and Information Security.

Editor's Picks

Free Newsletters, In your Inbox