Get a Message Of The Day in your PowerShell session

Sometimes you need an excellent inspirational quote to get some motivation 😉 In this blog post, I will show you how to get one each time you start a new PowerShell session.

How it works

The script will retrieve a random quote from a site containing many of them that don’t have any copyright. Eventually, you will get one that you have already seen, but there is little chance of that happening on the same day 😉

How it looks

When starting a new session, it will show you a Message Of The Day (MOTD) like this:

The script

The script is listed below. Save it somewhere as motd.ps1. It requires the PSParseHTML module to be installed. It will install it if needed, which could give you a one-time installation question.

#Retrieve quote from goldenquotes.net
#Get random numbers for the page number and quote number
$randompage = Get-Random -Minimum 1 -Maximum 7
$randomquote = Get-Random -Minimum 1 -Maximum 100

#Check if the module PSParseHTML is installed and install
#the module if it's not installed
if (-not (Get-Command ConvertFrom-HTMLClass -ErrorAction SilentlyContinue)) {
    Install-Module PSParseHTML -SkipPublisherCheck -Force:$true -Confirm:$false
}

#Get a random quote and display it
try {
    $page = Invoke-RestMethod -Uri "https://www.thegoldenquotes.net/best-100-public-domain-quotes-of-all-time-collection-0$($randompage)/"
    $convertedpage = ConvertFrom-HTMLClass -Class "siteorigin-widget-tinymce" -Content $page -ErrorAction SilentlyContinue
    $quote = "Message Of The Day:`n" + $($convertedpage)[$($randomquote)].substring(2)
    return $quote
}
#Show an error is there is an issue
catch {
    Write-Warning ("Error retrieving MOTD, please try again later...")
}

Add the script to your PowerShell profile

To start the script automatically when starting a PowerShell session, you can add it to your profile by:

- notepad $profile
- Add the line below, save and start a new session
. "C:\Data\motd.ps1" 

Download the script(s) from GitHub here

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.