Send Emails Easily Using Powershell v2 Cmdlet Send-MailMessage…

Most exchange admins would have written atleast a single script, maybe a very simple one. One of the most common scripts used in messaging environments is to email the exchange admin the total number of mailboxes in the organization (on a weekly basis for example) or to email the list of large mailboxes. You can…

Most exchange admins would have written atleast a single script, maybe a very simple one. One of the most common scripts used in messaging environments is to email the exchange admin the total number of mailboxes in the organization (on a weekly basis for example) or to email the list of large mailboxes. You can run powershell scripts to automate a number of processes and have that emailed to you.

Getting powershell to send an email was not straightforward in Exchange 2007, with powershell v1. We had to make use of the System.Net.Mail class and there are number of articles explaining the process.

With the release of Powershell v2, things get much easier. It has a new cmdlet for sending emails, “Send-MailMessage”. More info here

This cmdlet works in both Exchange 2007 SP2+ & 2010. Yes, I said 2007 SP2, provided that you have Powershell v2 installed. Check my previous article to know more about powershell v2 support for exchange 2007 SP2 here.

It is highly unlikely that you will have PS v2, unless you are creating a new environment from scratch. Exchange 2010 needs PS v2 by default and hence the cmdlet will run fine.

Let me explain with an example. I am running Exchange 2007 SP2 on Windows 2008 SP2 with Powershell v2 installed. I want to send an email from my account to my colleague Deepak with subject as “Test Email” and body as “Testing the cmdlet”. I ran the following in exchange shell.

Send-MailMessage –From Rajith@chakka.local –To Deepak@chakka.local –Subject “Test Email” –Body “Testing the cmdlet” –SmtpServer exch07.chakka.local

Send Email

The send-mailmessage cmdlet will work straight from a Powershell v2 console as well.

Send Mails PSv2

I checked Deepak’s OWA and there it is, the email that I had sent with the above syntax.

Check Email

Scripts get shorter with Powershell v2!

8 Comments

  1. Thank you very much, it helped a lot

    1. Rajith Jose Enchiparambil says:

      Glad the article helped you Shreyas.

    1. Rajith Enchiparambil says:

      Thanks Dinesh

    1. Rajith Enchiparambil says:

      Glad to help Chris.

  2. Rajith Jose Enchiparambil says:

    Thanks Glen

  3. Thanks Raj, I used this today!
    See you Monday :)
    Glen

Leave a Reply

Your email address will not be published. Required fields are marked *