Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Powershell Example: HashTables, SecureString, Splatting, Send-MailMessage
### Script Global Settings
#Declare SMTP Connection Settings
$SMTPConnection = @{
#Use Office365, Gmail, Other or OnPremise SMTP Relay FQDN
SmtpServer = ''
#OnPrem SMTP Relay usually uses port 25 without SSL
#Other Public SMTP Relays usually use SSL with a specific port such as 587 or 443
Port = 587
UseSsl = $true
#Option A: Query for Credential at run time.
Credential = Get-Credential -Message 'Enter SMTP Login' -UserName "emailaddress@domain.tld"
#Option B: Hardcoded Credential based on a SecureString
Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList @(
#The SMTP User Emailaddress
#The Password as SecureString encoded by the user that wil run this script!
#To create a SecureString Use the folowing Command: Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString
"Enter the SecureString here as a single line" | ConvertTo-SecureString
### Script Variables
#Declare Mailmessages.
$MailMessageA = @{
From = "emailaddress@domain.tld"
To = @(
#Cc = @(
# "emailaddress@domain.tld"
#Bcc = @(
# "emailaddress@domain.tld"
Subject = 'Mailmessage from script'
#Priority = 'Normal' #Normal by default, options: High, Low, Normal
#Attachments = @(
#InlineAttachments = @{
#} #For more information about inline attachments in mailmessages see:
BodyAsHtml = $true
Body = "Something Unexpected Occured as no Content has been Provided for this Mail Message!" #Default Message
### Script Start
#Retrieve Powershell Version Information and store it as HTML with Special CSS Class
$PSVersionTable_HTLM = ($PSVersionTable.Values | ConvertTo-Html -Fragment) -replace '<table>', '<table class="table">'
#Retrieve CSS Stylesheet
$CSS = Invoke-WebRequest "" | Select-Object -ExpandProperty Content
#Build HTML Mail Message and Apply it to the MailMessage HashTable
$MailMessageA.Body = ConvertTo-Html -Title $MailMessageA.Subject -Head "<style>$($CSS)</style>" -Body "
Hello World,
If your recieved this message then this script works.</br>
<div class='alert alert-info' role='alert'>
Powershell version
" | Out-String
#Send MailMessage
#This example uses the HashTable's with a technique called Splatting to match/bind the Key's in the HashTable with the Parameters of the command.
#Use the @ Symbol instead of $ to invoke Splatting, Splatting improves readability and allows for better management and reuse of variables
Send-MailMessage @SMTPConnection @MailMessageA

This comment has been minimized.

Copy link
Owner Author

SteloNLD commented Feb 16, 2018

InlineAttachments requires a custom Send-MailMessage command wich you can find here:


This comment has been minimized.

Copy link
Owner Author

SteloNLD commented Apr 19, 2018

BodyAsHTML = $True allows you to do 3 things:

  1. Use simple HTML Code to style and structure your Mail Messages.
  2. Use CSS Code such as Bootstrap For Email to style your Mail Messages
  3. Allows you to properly display powershell object(s) in mail messages as a table by using $Object | ConvertTo-Html -Fragment


To further enhance your HTML Mail Messages please take a look at Invoke-PsHTML, this allows you to create HTML Template Files with inline Powershell Code, a proper template can be found at Bootstrap For Email

Contact me at if you are interested in amping up your Powershell Reports / Mail Messages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.