Send Automated Email with PowerShell

I am supposed to send a weekly status report to my team lead and manager every week.  I really like that the company takes an effort to know what is going on with the employees, and I like that I have the ability to speak up as soon as an issue arises.  However, I almost always forget to send this email.  I have set an alarm, and I am almost always busy when it goes off.  By the time I finish what I am doing, I forget the write the email and then it doesn’t get done.  I thought a fun project would be to create a PowerShell script to email me every Friday with a randomly generated status update.  I don’t want to send this randomly generated status automatically to my boss (just in case one week I want to add something to the status), so I will have it email me at work and my personal email.

The update has two parts: how I feel about my work and how I feel about my department.  For each of these, I wrote a few ‘beginning’ sentences and a few ‘ending’ sentences.  The script picks a random beginning and ending sentence for each category (work and department), color codes it, and sends the email to my personal and my work emails.

First, I set the email addresses and the subject:

Next, I set my work beginning sentence list and choose a random entry from it.  This general syntax is repeated a total of 4 times (two for work and two for department):

Now we can set up the body.  Part of the requirement for my status report is that each section (work/department) has to be color coded (green/yellow/red).  I want to default this to green.  In order to do that, we’ll have to write a little bit of HTML in the body of our email to highlight the text.  The body variable looks like this:

This is pretty simple HTML.  We are setting a style tag to highlight text as lime.  We will highlight just the $work and $department variables (but we will leave the ‘My Relationship’ headings).

Finally, we will set up the gmail SMTP server and port variables:

Now, we will set up our Gmail username and password and send the message:

Note that for the password string, you have to use ConvertTo-SecureString for this to work!

The last step was to simply set this script to run automatically every Friday at 2 pm.  To do this, I set up a task in the task scheduler (which we discussed here).  The only tricky part was that the action needed to start a program, with the program variable ‘powershell’ and the arguments -file “filelocation”.

That’s it!  Hopefully this will help me remember to send this every week!

Have questions or suggestions?  Please feel free to comment below or contact me.

One thought on “Send Automated Email with PowerShell

Leave a Reply

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