Automate Work with PowerShell – Sending Email

In this blog post I am going to show you how you can use PowerShell to automate your work. I have begun using PowerShell more and more at work and found how helpful it can really be. This is something I use all the time to automate sending the same type of emails. If you have to send out emails through Outlook all the time then this automation tip will help you.

PowerShell – needed to run the script
Outlook – email client used to send email
Script – I will include the script is use in the description down below.

Get Started:
So to get started open you editor of choice I use Visual Studio Code but any editor will work. My script uses input boxes instead of using the PowerShell window for input. You can choose to do either however you want.
To setup an input box we use a function and variables to show the input box and record the results. I use this input box function on many of my scripts to improve the input options  if you like it feel free to use it for your scripts.

In the function shown you don’t have to edit anything but you can change the Title if you want to customize it. After the function we have to set the variables we are going to use to send the email.

The input variables I use are for the email recipient, subject and ticket number. Outside of the first two which you will want to keep you can add additional variables depending on the email purpose.

In the next part of the script you can set what you want to be in the body of the email and include any variables you have entered through the input box. This is just setting it as a variable right now and then it will be sent to Outlook to send the email.

The last part of the script is the most important, it takes all the variables entered and connects to Outlook and sends the values for each variable to the correct part of the message. The reason this only works on Outlook is because it is creating a Outlook specific object to transfer the variables into a new email message.

I found this online at Stack Overflow if anyone is curious about the Outlook connection.

The first lines creates a new object to connect to Outlook, it then sets the namespace to MAPI. The next two lines create the mail message and activates it so we can write to it. I also have a signature in my email so the next line gets the HTMLBody which before anything is added is just the signature that is preloaded on all new messages. In the next few lines it adds it all together, starting with the mail recipient, then subject, then body which includes the body and signature variables above and then saves the newly created email to be ready to send.

So now that we have written the scripts here’s what it looks like when running it:

After you enter the script in a PowerShell window the first input box asks for email address and enter the address and click OK.

Input Box to enter recipient email address.

Once you hit OK you will be prompted for the message subject the click OK.

Enter Message Subject

If you have another prompt like me enter the necessary information and click OK.

Then the Outlook new email box will open and with your data already filled in you can edit and then Send.

That’s it the script is now complete, a few things of note first this is an example and not how I use it in the real world in case you were wondering. Also this should work with most of the versions of PowerShell and Outlook so you don’t have to get the latest for this to work. If you want to just download the script instead of making it yourself a link to it is below. I hope this helps you automate your day and let me know if you have any questions or issues.


Leave a Reply