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.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
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.