Send an e-mail with PowerShell

I’ve harped on learning PowerShell a few times on this blog. Here’s a quick script that you may find useful once you get started using it!

Photo by Allie Smith on Unsplash

It’s no secret that I love PowerShell! And once you get started using it, you’ll love it too. That’s because if you’re in a Windows shop (that is to say, you work in a place that is mostly a Windows environment,) PowerShell integrates with just about every Microsoft product out there. I think of PowerShell as a unified language that I can use to speak to most Microsoft products.

This is powerful because you can write scripts to automate just about anything in a Windows environment and you can involve several different disparate systems. For example, I could create a script that pulls all my users from Active Directory (a Microsoft product) and emails all of them using my Exchange server (another Microsoft product.) Neat!

One thing you’ll quickly start doing with PowerShell is creating simple scripts to do a thing, and then you’ll set them up on some kind of automation platform to run at a certain cadence.

When I first started doing this, I found it was a chore to log on to the automation platform or server that was running the script to see if it had run successfully. It was tedious, I often forgot to actually log on to check, and once I had a few scripts running at the same time it became impractical. So, I decided to have my scripts email me to let me know if they had run successfully or failed.

One note about this, though. It will only work if your Exchange server allows SMTP relaying, which is what we’re leveraging here. SMTP relaying is when an Exchange server allows anyone to send it an SMTP request and forwards it on. SMTP is a protocol used for sending emails. You can ask a systems administrator at your company if SMTP relaying is allowed. Not all companies allow it because it can be misused, as we’ll see later.

You can add the following code snippet to any script you create and adapt it to your purpose. For example, in the output variable below, you could write “the script ran successfully.” Or, you could get more complex and have the output be the results from your script’s run. Sticking with the example from above, you could add each user’s name to the output as each message is sent. At the end, you would have an email sent that reads:

“The following recipients were e-mailed:

Bob

Sue

Joe

Jill”

You could also modify the output depending on the success of the script. In pseudocode (which means that this is not real code but the ‘plain English’ version of the code,) that would look like this:

If (script ran successfully)

Then (output = “The script ran successfully”)

Else (output = “The script failed. Debug it!”)

Now, on to the script. But first, a quick word of warning: you should never run a script you found on the Internet in your environment without understanding it fully! PowerShell can be very unforgiving and could wreck your world very easily!

Here’s the script, and then we’ll talk about it at a high level so you understand what it’s doing and what you need to do to modify it. PowerShell allows you to write comments in your code for clarity, so I’ll do that below. Anything after the # sign is not code, it’s a comment and won’t be run by the script.

#The script starts here. I put the output and user variables at the top since these get modified every time you want to use this snippet.

$Output = “The script ran successfully”

$User = “Sachatememe”

#This part contains the details needed to actually send the email

$email = @{

#You can actually put anything here. This is called email spoofing. Have fun with the names, but don’t use this maliciously! More on that later.

From = “ScriptRunner3000@thesiliconpath.com”

To = “$user@gmail.com”

Subject = “Your fancy script ran”

#You’ll need to find out from a systems administrator what your company’s SMTP server is and replace the below with your server details.

SMTPServer = “mail.thesiliconpath.com”

#This is the output we wrote in above.

Body = $Output

}

Send-MailMessage @email

Easy! Now let’s take a look at what’s going on. I’ll translate the above in pseudocode.

The output of the email = “The script ran successfully”

The user to send the email to = “Sachatememe”

Here is the relevant information the script will need to send the email {

Mark the sender field of the email as coming from = “ScriptRunner3000@thesiliconpath.com”.

Send it to the user we specified above and add @gmail.com to the end.

#PS: that gives you sachatememe@gmail.com – my email address. Feel free to drop me an email or leave me a comment below!

Set the subject of the email to “Your fancy script ran”.

Use this server to send the email: mail.thesiliconpath.com.

The body of the email should be the contents of the output variable.

}

Send the message now.

Now you have this simple but useful tool in your scripting arsenal! Please use it for legitimate purposes though – as I showed you above, this can be used to spoof email addresses. Doing this to send emails outside of your company while using your company’s email server can get both you and your company in trouble, so, you know, don’t do it.

Now go write some PowerShell!

With each post, I cover a new topic to help you get your start (or keep progressing) in your IT career. If it’s your first time visiting this blog, start here. Or, see all my posts about interview questions you should definitely be prepared for.

Author: Silicon Wanderer

I'm a merry wanderer on the path to financial independence through IT. I'm doing it, and I want to show you how you can to!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s