Add a Bit of Color to PowerShell

I’ll admit that using color in PowerShell is more of an afterthought for most scripts, it does come in handy for certain situations. One could use color to enhance output to the screen to visually indicate something good (green), bad (red) or warning (yellow). Other potential uses would be in menus to help delineate options on the menu. In this tip we’ll explore easy ways to add color and what color options are available to us.

First. What Colors?

Picking a good color is truly in the eye of the beholder. Some colors are more universal, like the three I mentioned earlier, but others could also be used for personal tastes. One thing to watch out for is the background color of our PowerShell console. Some colors are either a horrid combination with the background color or they might disappear (blue on Windows PowerShell and black for Exchange PowerShell) for some exampled. Here are the currently available colors in PowerShell:

Exchange PowerShell console (black background):

Windows PowerShell console (blue background):

As we can see, some colors show up better than others. Now we can always adjust the background color to something like white:

Which is apparently a bad idea as well. Or maybe a light gray:

All of the above certainly illustrate that, whatever the color that is chosen, know what they background color is as your results may be good, or just entirely not readable. Choose carefully.

Now on to the Color Code! Now that we have that out of the way, let’s quickly explore some sample color coding for PowerShell output (and other uses).

Error Messages:
[sourcecode language=”powershell”]
Write-Host "Failed" -ForegroundColor Red
Write-Host "Cannot acquire the amount of RAM in the server." -ForegroundColor Red
Write-Host "Server Management module could not be loaded." -ForegroundColor Red
[/sourcecode]
Warning Messages:
[sourcecode language=”powershell”]
Write-Host " not detected! " -ForegroundColor Yellow -NoNewline
Write-Host ' –> Pagefile is too SMALL' -ForegroundColor Yellow
[/sourcecode]
Success
[sourcecode language=”powershell”]
Write-Host "Passed" -ForegroundColor Green
[/sourcecode]
Menus (Combining multiple colors)
[sourcecode language=”powershell”]
$Menu = { Write-Host " *****************************************************" -ForegroundColor Cyan
Write-Host " Exchange Server 2019 (Full OS) Prerequisites Script" -ForegroundColor Cyan
Write-Host " *****************************************************" -ForegroundColor Cyan
Write-Host " "
Write-Host " *** For .NET 4.8 use Option 23 prior to any other options ***" -ForegroundColor Yellow
Write-Host " "
Write-Host " Install NEW Server" -ForegroundColor Cyan
Write-Host " ——————" -ForegroundColor Cyan
Write-Host " 1) Install Mailbox Role Prerequisites" -ForegroundColor White
Write-Host " 2) Install Edge Transport Prerequisites" -ForegroundColor White
Write-Host " "
Write-Host " Prerequisite Checks" -ForegroundColor Cyan
Write-Host " ——————" -ForegroundColor Cyan
Write-Host " 10) Check Prerequisites for Mailbox role" -ForegroundColor White
Write-Host " 11) Check Prerequisites for Edge role" -ForegroundColor White
Write-Host " 12) Additional Exchange Server checks" -ForegroundColor White
Write-Host " "
Write-Host " One-Off Installations" -ForegroundColor Cyan
Write-Host " ———————" -ForegroundColor Cyan
Write-Host " 20) Install – One Off – Microsoft C++ 2013" -ForegroundColor White
Write-Host " 21) Install – One Off – Microsoft C++ 2012 (Mailbox/Edge Transport)" -ForegroundColor White
Write-Host " 22) Install – One Off – UCMA 4.0" -ForegroundColor White
Write-Host " 23) Install – One-Off – .NET 4.8 – CU2+" -ForegroundColor White
Write-Host " "
Write-Host " Additional Configurations" -ForegroundColor Cyan
Write-Host " ————————-" -ForegroundColor Cyan
Write-Host " 30) Set Power Plan to High Performance" -ForegroundColor White
Write-Host " 31) Disable Power Management for NICs." -ForegroundColor White
Write-Host " 32) Add Windows Defender Exclusions"
Write-Host " 33) Configure PageFile to 25% of RAM" -foregroundcolor green
Write-Host " 34) Configure Event Logs (App, Sys, Sec) to 100MB" -ForegroundColor White
Write-Host " 35) Configure TCP Keep Alive Value (1800000)" -ForegroundColor White
Write-Host " " Write-Host " 40) Launch Windows Update" -ForegroundColor White
Write-Host " 41) Clear Windows Defender Exclusions" -ForegroundColor White
Write-Host " 42) Report Windows Defender Exclusions" -ForegroundColor White
Write-Host " "
Write-Host " Exit Script or Reboot" -ForegroundColor Cyan
Write-Host " ————————-" -ForegroundColor Cyan
Write-Host " 98) Restart the Server" -ForegroundColor Red
Write-Host " 99) Exit" -ForegroundColor Cyan
Write-Host " "
Write-Host " Select an option.. [1-99]? " -ForegroundColor White -nonewline
}
[/sourcecode]
This in turn looks like this when run in PowerShell:

Now, the above examples are not exhaustive, but just some to provide you with practical examples of color usage in PowerShell.

Related Post