Send-MailMessage parameters and examples

In this post, I will talk about commonly used parameters of the Send-MailMessage PowerShell cmdlet and a few examples of how to use them.

Update: We have a new post that explains how to send an email with PowerShell in more detail.

Most of the automations that system administrators write generally involve sending email at some point in the script, whether to report errors or to send consolidated output. Given the high usage of this functionality, I would like to describe some of the common use cases of the Send-MailMessage cmdlet (a new cmdlet introduced in PowerShell 2.0).

Send-MailMessage parameters ^

For any cmdlet, we first need to understand the parameters that we should pass to it to get the desired functionality/output. Like any other cmdlet, Send-MailMessage also has both mandatory and optional parameters. I will talk about the ones that are used most.

-FROM: Every email needs a “from” address. Without this, email cannot be sent. Though it is a required parameter, the email ID doesn’t necessarily have to be a valid one. You can use and the cmdlet will still work.

-TO: This is also a mandatory parameter. You can supply a single or multiple email addresses, and you can also specify the name associated with the email address (for example, -To “Sitaram Pamarthi <sitaram>”).

-SMTPServer: You need to provide the name of the SMTP server through which you want to send the email. If you are in doubt, ask your email administrator for the server to use. If this parameter is not specified, PowerShell tries to pick the value from the $PSEmailServer preference variable, if this variable is set.

-CC and -BCC: You know what they are. If you would like to CC or BCC somebody in the email, pass their email addresses to these parameters.

-Attachments: This parameter takes the full path of the file(s) that you want to attach to the email.

-Subject: This is a simple string that represents the subject of your email.

-Body: This string specifies the body of your email message.

Send-MailMessage examples ^

We have seen some of the frequently used parameters. Now let me take you through some examples, which will come in handy when writing your scripts.

Send email to one recipient

Send email to multiple recipients in To and CC

Send email to one recipient with an attachment

Send email to one recipient with multiple attachments

Send email via an SMTP relay server that requires authentication

The above command will prompt for the credentials and send the email after the credentials are validated.

Send status of all services in a server as an email

Send high-priority email

These use cases are just a few examples. You can do much more with this cmdlet, such as send emails over TLS channels (-UseSSL parameter) or request delivery receipts for emails that you send (-DeliveryNotificationOption parameter).

To obtain help for this cmdlet, type the following command in your PowerShell window:

Update: We have a new post that explains how to send an email with PowerShell in more detail.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

  1. Karthik 4 years ago

    How do I send mail when I do not know the SMTP server host, but know only the LDAP host name


    • Without SMTP server you can't send an email.


    • Huy M Le 3 years ago

      If you have an email account from anywhere then you can send email.

      To find out the SMTP server you just go to,

      from there enter in you domain name (ie where your email is (  This will return you the  MX server record (SMTP).  From there you just copy that address as your SMTP server.  You may have to use (credential) option on the PowerShell Command to specify smtp server and have to input your username/password (email + password).  That should work fine.


  2. addison 4 years ago

    Thank you.  This was very helpful.


  3. Sam 4 years ago

    How do I ensure that the from name field that the recipient sees is customized as per the value I provided.
    I have a requirement where I send emails to multiple people but I don't want them to reply to me and spam my mailbox. I tried using '' in the from field but it still ends up with the recipient with my actual email id.

    I have powershell 4 and our smtp server is google (enterprise gmail service).


    • Try another SMTP server. I guess Google's mail server replaces the contents of the from field with your email address. If you ask, they might tell you that they do this to prevent spamming. In reality they want you to pay for another mailbox.


  4. Rick 4 years ago

    I am trying to add a internet header (self-created one, call it x-abc) how would I code that in powershell in the send-mailmessage?


  5. Rick 4 years ago

    This is Rick, what I am trying to do is to add a custom x-header into an outgoing e-mail using the send-mailmessage. The e-mail will be sent via the task scheduler in Win 2012.


  6. Shalaka 3 years ago

    hi, I am using a variable that would have  the list of files to be attached (dynamically generated bases on some other logic) that I want to send to the -Attachements...but it does not seem to work.

    My variable:

    $attachment = "C:\......pdf","C:\.....pdf"

    Then in the send mail I have

    Send-MailMessage `
    -To $emailarray `
    -From ..
    -SmtpServer ...
    -Subject "$subject" `
    -Body "$body" `
    -Attachments $attachment `


  7. Eman 2 years ago

    how can i select 2 multiple random attachments from a folder?


  8. Mike D 2 years ago

    Instead of listing each email address in the -To parameter, is it possible to set this up to read a .txt file of email addresses, or even better a .csv list of emails?


    • If your CSV file looks like this:

      you could use a script that looks like this:


  9. Terry Bennett 2 years ago

    how can I do error checking on Send-MailMessage?

    IF the message was sent successfully, continue, otherwise, do it again, etc, etc.





  10. kumaraswamy 1 year ago

    while sending  mail through powershell , im getting below error:

    Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated



  11. Chris 1 year ago

    I'm writing a script that does 3 things.

    1. Scans the D:\ drive for viruses
    2.  Scans the D:\  drive for medical records
    3.  Sends out an email with the same subject and slightly different body but the recipient will change. How can I specify a different recipient when running the script
    4.  Bonus points if you also can share how I'd be able to edit the body of the email directly in the shell session when running the script. (just the greeting of the email will change for example email #1 starts off with Dear John and email #2 starts off with Dear Jane)




  12. tika 1 year ago

    Hi, I'm trying to send a mail-template file as the body of the message.  How can I do that?




    • You cant do that. The Body parameter only accepts string. You can only create a HTML code and put it as the Body with -BodyAsHTML parameter. File can be only added to attachment with -Attachment parameter.


  13. Cookie 4 months ago

    I had an issue using a variable which contained multiple addresses. Using -To "","" works, but when these where in a variable it didn't work (-To $To)

    I solved it using:

    [string[]]$To=[system.String]::Join(",", $To)

    where $To contains multiple addresses.

    In the send-mailmessage command use -To ($To -split ',')

    You can also use this for Cc and if you want to add multiple attachments from a variable to your email


Leave a reply

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


© 4sysops 2006 - 2020


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account