#requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Out-Rainbow.ps1 # Version: 1.0 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # Date: 1/21/2011 # Keywords: # Comments: # This function works best with the PowerShell console. It will # work with the ISE but the background color won't be filtered out. # # "Those who neglect to script are doomed to repeat their work." # # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** # ----------------------------------------------------------------------------- Function Out-Rainbow { <# .Synopsis Write rainbow colored output to the console. .Description This function takes string input and writes each word in a random color to the host. The function works best with the PowerShell console. .Parameter Message The string of text to colorize. .Parameter Background Temporarily change the background color. The screen will be cleared. At the end of the command the background color will be changed back and you will manually need to run Clear-Host. Valid colors are: "Black","DarkMagenta","DarkRed","DarkBlue","DarkGreen","DarkCyan", "DarkYellow","Red","Blue","Green","Cyan","Magenta","Yellow","DarkGray","Gray","White" .Example PS C:\>"Take the first step in faith. You don't have to see the whole staircase,", "just take the first step.", " --Martin Luther King, Jr." | out-Rainbow .Example PS C:\> (get-process w* | out-string).Split("`r") | out-rainbow You'll get better results by converting the output to a string and then splitting it so that each line is processed completely by the function. .Notes NAME: Out-Rainbow AUTHOR: Jeffery Hicks VERSION: 1.0 LASTEDIT: 01/21/2011 Learn more with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010) .Link http://jdhitsolutions.com/blog/2011/01/friday-fun-out-rainbow/ .Link Write-Host .Inputs Strings .Outputs None #> Param ( [Parameter(Position=0,Mandatory=$False,HelpMessage="Enter a message to display.", ValueFromPipeline=$True)] [string[]]$Message, [Parameter(Mandatory=$False,ValueFromPipeline=$False)] [ValidateSet("Black","DarkMagenta","DarkRed","DarkBlue", "DarkGreen","DarkCyan","DarkYellow","Red", "Blue","Green","Cyan","Magenta","Yellow", "DarkGray","Gray","White")] [string]$Background=$Host.UI.RawUI.BackGroundColor ) Begin { #measure the $Message variable. A value of 0 means it was piped in #and a value greater than 0 means it as passed as a parameter #This has an affect on the processed output. if (($message | measure-object).Count -eq 0) { Write-Verbose "Pipelined input" $Pipelined=$True } #define the array of valid colors $AllColors=@( "Black","DarkMagenta","DarkRed","DarkBlue", "DarkGreen","DarkCyan","DarkYellow","Red", "Blue","Green","Cyan","Magenta","Yellow", "DarkGray","Gray","White" ) Write-Verbose "Omitting $($host.ui.RawUI.Backgroundcolor) from color array" #filter out the current background color $colors=$AllColors | Where {$_ -ne $host.ui.RawUI.Backgroundcolor} #Check if -Background was called and if the value is different than the current #background. if ($Background -ne $Host.UI.RawUI.BackgroundColor) { #save current color $SavedBGColor=$Host.UI.RawUI.BackgroundColor #set background color $Host.UI.RawUI.BackgroundColor=$Background #need to clear the host for this to work properly Clear-Host Write-Verbose "Changed background color ro $Background" } } #close Begin Process { ForEach ($m in $Message) { Write-Verbose "Splitting message $m" #split the line into a word array $arr=$m.Split() Write-Verbose "Now have $($arr.count) words" for ($i=0;$i -lt $arr.count;$i++) { #write each word followed by a space. Don't insert a new line Write-Host "$($arr[$i]) " -foregroundcolor $colors[(Get-Random -Min 0 -max ($colors.count-1))] -NoNewline } #for if (-Not $Pipelined) { #if not pipelined input then add a carriage retrun Write-host `r } } #foreach #add a carriage return after each processed object write-host `r } #close Process End { #change the background color back if there is a saved color if ($SavedBGColor) { Write-Verbose "Returning background color to $SavedBGColor" $host.UI.RawUI.BackgroundColor=$SavedBGColor } #write a carriage return for visual clarity Write-Host `r Write-Verbose "We've reached the end of the rainbow" } #close End } #end function