How to Send Email with Attachment via Exchange PowerShell
This article will show you how to send an Email, with attachment, within your domain by using the Send-MailMessage command and sending it directly through a HUB server.
First, let’s create the PS1 script:
Copy and paste the following code into notepad and save as (Save as type: all files) with the name SendEmail.ps1. You will need to change the text in bold to the appropriate HUB server IP address, the SMTP of who you are sending to, the SMTP of who you want it to be from, and then of course the subject and body to be displayed in the Email.
#CODE STARTS HERE
#Written by Doug Spear
#MSExchangeGuru.com
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$file = “c:\file.csv”
Send-MailMessage -SmtpServer HUB.SERVER.IP.HERE -To COWORKER.EMAIL@DOMAIN.COM -From YOUR.EMAIL@DOMAIN.COM -Subject “SUBJECT HERE” -Body “BODY HERE” -attachment $file#CODE ENDS HERE
Now I know what you’re saying, that’s cool but not that cool…
Well, how about we make it cool. Rather than just having an existing file to send as an attachment, think of the possibilities if you have data exported to the file prior to the Send-MailMessage command (change USERHERE in bold below to a valid mailbox):
Get-MailboxStatistics USERHERE | select-object DisplayName, ItemCount, TotalItemSize, TotalDeletedItemSize, LastLogonTime, LastLogoffTime | export-csv “c:\file.csv” -NoTypeInformation
Now you have a script that looks like this:
#CODE STARTS HERE
#Written by Doug Spear
#MSExchangeGuru.com
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
Get-MailboxStatistics USERHERE | select-object DisplayName, ItemCount, TotalItemSize, TotalDeletedItemSize, LastLogonTime, LastLogoffTime | export-csv “c:\file.csv” -NoTypeInformation
$file = “c:\file.csv”
Send-MailMessage -SmtpServer HUB.SERVER.IP.HERE -To COWORKER.EMAIL@DOMAIN.COM -From YOUR.EMAIL@DOMAIN.COM -Subject “SUBJECT HERE” -Body “BODY HERE” -attachment $file#CODE ENDS HERE
Now you have a simpler way of sending yourself, or your boss, mailbox statistics. This is just the beginning of the endless possibilities with this simple script!
Note: This scirpt is mainly to be used in Exchange PowerShell. The main “meat and potatoes” script using the Send-MailMessage command will work for Windows PowerShell without piping the Exchange data and is fun to use for testing.
October 10th, 2013 at 10:09 am
My powershell doesnt recognize the command “Get-MailboxStatistics” ! I’m going nuts ! Can you please tell me how to install this snapin ? Thanks !
November 25th, 2013 at 10:36 am
Hi DC,
You should be using Exchange management shell or import or load exchange module.
team@MSExchangeGuru
April 20th, 2015 at 9:30 am
Hi to all,
I have used a simple script with above command for “send-mailmessage…..”
I wan to make the subject in BOLD.
How to do it?
Your comments will be very helpful
May 7th, 2015 at 3:38 am
I don’t think subject can be set to bold.