It’s late on a Friday night. Like most people, you have somewhere else you would rather be. Instead, you are stuck at
work because you completely forgot about some scheduled maintenance that has to be done that night.
You’re familiar enough with PowerShell to make some scripts to take care of the maintenance, but you’re worried about the off-chance that one of the scripts fails or you’re worried that something could go wrong.
Wouldn’t it be great if you could start your scripts and configure them to email you the results? You could still get out, enjoy your Friday night and rest assured that your scripts are doing their job without you losing yours!
(Disclaimer: We do not condone the shirking of any responsibilities that you may have…especially on Friday nights.)
Sending Email with PowerShell (Send-MailMessage)
There are a multiple ways to send an email with PowerShell. This is a native cmdlet option that is simple and easy to use. It uses the cmdlet Send-MailMessage.
List of available parameters
The full path and filenames to be attached to the email.
Email addresses that you would like to bcc.
The body of the email message.
Indicates that the value of the Body parameter contains HTML.
Email addresses that you would like to cc.
An account that has permission to send the email. The default is the current user.
Delivery notifications will be sent to the email address specified in the –From parameter.
None – No notification (default)
OnSuccess – Notify if the delivery is successful.
OnFailure – Notify if the delivery is unsuccessful.
Delay – Notify if the delivery is delayed.
Never – Never notify.
The encoding used for the body and subject.
The address from which the mail is sent.
The priority of the email message. Valid values: Low, Normal (default), High
The name of the SMTP server that sends the email message.
The subject of the email message.
The addresses you wish to send email to.
Use the Secure Sockets Layer (SSL) protocol to establish a connection to the SMTP server to send email. SSL is not used by default.
Examples of Usage:
Example 1 – Sending a simple email
Example 2 – Sending an email from Gmail
Here’s a full example of sending an email via Gmail’s SMTP servers. It will prompt you for a username and password thanks to (Get-Credential). The username needs to be and password needs to be , though you could always pipe in a PSCredential and get the same effect.
Best of luck to you in your emailing endeavors!