Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun Send a Colorful Message

Posted on April 18, 2014

Next week is Pluralsight's 10th anniversary. In preparing for that happy event, I wanted to send a special greeting. Of course, because my courses are on PowerShell it only seemed appropriate to use PowerShell to display my message. In fact, let's jump right to the result.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

HappyBirthdayPluralsight

Here's how I did it.

#requires -version 3.0

#get possible colors but exclude whatever is being used for the current background
$colors = [enum]::GetNames([consolecolor]) | where {$_ -ne $host.ui.RawUI.BackgroundColor}

#the message to be displayed
$msg=@"
************************************
*                                  *
*   Happy Birthday, Pluralsight!   *
*                                  *
************************************
"@

#write each character individually
$msg.ToCharArray() | 
foreach -begin {
Clear-Host
#define a hashtable of parameters to splat to Write-Host
$paramHash=@{NoNewLine=$True;}
} -process { 
   #use a color if not a space
if ($_ -notmatch "\s") {
    $paramhash.ForegroundColor = (Get-Random $colors)
    $paramhash.BackgroundColor = do {
     #get a background color that is different than the 
     #foregroundcolor
     Get-Random $colors
     } until($_ -ne $paramhash.ForegroundColor)   
 } #if not whitespace
 else {
    #clear colors
    $paramhash.Remove("ForegroundColor")
    $paramhash.Remove("BackgroundColor")
 }
  #write each character to the host
  $_ | Write-Host @paramhash

} -end {"`n"}

#no puppies were harmed in the making of this script

The script takes a string, in this case a here string stored as $msg, and writes each character to the console using Write-Host. I'm using Write-Host so that I can take advantage of foreground and background colors. I get a random color for each from a list of possible console colors, skipping the color used for the current console background. My script uses a Do loop to get a random color for the background that is different than what is chosen for the foreground. I only use a color scheme if there is a non-space character. I suppose I could have turned things around and tested with -match against \S.

Anyway, a short and simple script that gets the message across in a colorful way. Enjoy and have a great weekend.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d