• Ash K.

Creating and Sending Emails with HTML/CSS and Powershell

Audits are the bread and butter for sysadmins. License consumption, user activity, server activity, network devices, etc. My favorite way to send this information is via attractive and brand-styled emails using Powershell in Scheduled Tasks or Azure Automations. I do this using a combination of HTML/CSS and Powershell.


For this post, you'll want to have some familiarity with the basics of HTML and CSS, and of course Powershell. If you need a quick tutorial on HTML/CSS, check out this website.


In this post, we'll walk through how to create and send stylized emails in Powershell using HTML and CSS.

The directions below will start with how to style your email with CSS, create HTML tables with Powershell, define interesting text options with HTML, and finally how to send the email!


1. Define your style with CSS

The CSS style will set the mood for your entire email, so it's important to get this part right! This is where you can be creative with your colors and fonts. The CSS below is very simple, and based on the colors and fonts in this website. You'll see screenshots below of the results.


When you're writing your CSS, you can use this website for some quick styling tips.


2. Display your information using a Powershell array

I love using tables in emailed audits. It's stylish, and supplies a lot of data in a quickly digestible format. There's a couple different ways to do this, but this one is my favorite because it looks clean. You can use any variable in the "Value N".


After creating the array, you'll convert the array into an HTML table using "ConvertTo-HTML":


Here's how to write your values if you're using arrays as values and want them to show up as a list:


If you're going to use multiple tables in your email, you'll need to adjust the formatting a little to use the "ConvertTo-HTML -Fragment". To learn a little more about why that's necessary, check out this article.


Here's a screenshot of a styled email with two tables:


3. Grab your readers with interesting text in HTML

While CSS defines the style for the email, HTML does everything else, including creating titles, font sizing, etc. Here's the code for some quick HTML text options:


All of these elements look like this when interpreted by the email client:


4. Sending the email

After styling and formatting your email, all you have left to do is send it! Use the code below to send an email using an O365 account:


5. Putting it all together

Here's the full script. You can also find it on my GitHub here.


©2019 by KickA5h Admin

All code provided on this website is done so with the confidence that you know never to execute untrusted code in your environment.