Get a Message Of The Day in your PowerShell session

Sometimes you just need a nice 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 a lot of them which 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 it install it for you 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-Host "Error retrieving MOTD, please try again later..." -ForegroundColor Red
}

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.