PowerShell

PowerShell – How to format Write-Host with multiple colors

When you begin to write PowerShell scripts you usually just want it to do some simple tasks and automations. But after a while some scripts grow so large that you actually want to have them output in a bit nicer way.

PSWriteColor Information
Please notice this article contains parts of information (still useful) and may not reflect all functionalities of this module. For download, source code and so on you should refer to the dedicated PSWriteColor module page. After reading this one… of course! It contains useful informationexamples and know-how.
Problem Description

Usually to output information in PowerShell we use Write-Host. By using parameters ForegroundColor and BackgroundColor parameters you can define nice looking output text.

Write-Host "Whole text is in green" -ForegroundColor Green
Write-Host "Whole text is in red" -ForegroundColor Red
Write-Host "Whole text is in white" -ForegroundColor White

Write-Host "Whole text is in red with background Yellow" -ForegroundColor Red -BackgroundColor Yellow
Write-Host "Whole text is in yellow with background Dark Green" -ForegroundColor Yellow -BackgroundColor DarkGreen

While it's usually good enough for most scripts sometimes formatting one line of script with multiple colors is required. Wouldn't it fun to have Green Red Yellow outputed by PowerShell script?

You can actually do that with 3 “simple lines.

Write-Host "Green " -ForegroundColor Green -NoNewline; 
Write-Host "Red " -ForegroundColor Red -NoNewline; 
Write-Host "Yellow " -ForegroundColor Yellow -NoNewline;

Or even have it as one-liner:

Write-Host "Green " -ForegroundColor Green -NoNewline; Write-Host "Red " -ForegroundColor Red -NoNewline; Write-Host "Yellow " -ForegroundColor Yellow -NoNewline;

While this works it makes code very hard to read!

Solution

But there's actually  a nice and easy way to make it work without much effort. By adding Write-Color function you give yourself couple of options. For example:

Multiple colors in single line, if it's short enough

Write-Color -Text "Red ", "Green ", "Yellow " -Color Red,Green,Yellow

Or if it's a bit longer…

Write-Color -Text "This is text in Green ",
                   "followed by red ",
                   "and then we have Magenta... ",
                   "isn't it fun? ",
                   "Here goes DarkCyan" -Color Green,Red,Magenta,White,DarkCyan

And the colorful output from this PowerShell method:

This method actually has couple of other features. It allows easy adding of tabbing, lines before and after output. It's useful in creating menu's where it doesn't clutter the output too much. You can for example create:

Options menu

Write-Color "1. ", "Option 1" -Color Yellow, Green
Write-Color "2. ", "Option 2" -Color Yellow, Green
Write-Color "3. ", "Option 3" -Color Yellow, Green
Write-Color "4. ", "Option 4" -Color Yellow, Green
Write-Color "9. ", "Press 9 to exit" -Color Yellow, Gray -LinesBefore 1

Or adding a tab before your line

Write-Color -Text "This is text in Green ",
                "followed by red ",
                "and then we have Magenta... ",
                "isn't it fun? ",
                "Here goes DarkCyan" -Color Green,Red,Magenta,White,DarkCyan -StartTab 3 -LinesBefore 1 -LinesAfter 1

Or adding 2 lines, with 2 empty lines before it

Write-Color -LinesBefore 2 -Text "This little ","message is ", "written to log ", "file as well." -Color Yellow, White, Green, Red, Red -LogFile "C:\testing.txt" -TimeFormat "yyyy-MM-dd HH:mm:ss"
Write-Color -Text "This can get ","handy if ", "want to display things, and log actions to file ", "at the same time." -Color Yellow, White, Green, Red, Red -LogFile "C:\testing.txt"

And the output of final script:

Write-Color -Text "Red ", "Green ", "Yellow " -Color Red,Green,Yellow

Write-Color -Text "This is text in Green ",
                "followed by red ",
                "and then we have Magenta... ",
                "isn't it fun? ",
                "Here goes DarkCyan" -Color Green,Red,Magenta,White,DarkCyan

Write-Color -Text "This is text in Green ",
                "followed by red ",
                "and then we have Magenta... ",
                "isn't it fun? ",
                "Here goes DarkCyan" -Color Green,Red,Magenta,White,DarkCyan -StartTab 3 -LinesBefore 1 -LinesAfter 1

Write-Color "1. ", "Option 1" -Color Yellow, Green
Write-Color "2. ", "Option 2" -Color Yellow, Green
Write-Color "3. ", "Option 3" -Color Yellow, Green
Write-Color "4. ", "Option 4" -Color Yellow, Green
Write-Color "9. ", "Press 9 to exit" -Color Yellow, Gray -LinesBefore 1

Write-Color -LinesBefore 2 -Text "This little ","message is ", "written to log ", "file as well." `
            -Color Yellow, White, Green, Red, Red -LogFile "C:\testing.txt" -TimeFormat "yyyy-MM-dd HH:mm:ss"
Write-Color -Text "This can get ","handy if ", "want to display things, and log actions to file ", "at the same time." `
            -Color Yellow, White, Green, Red, Red -LogFile "C:\testing.txt"

And log file data properly written to file (it's additive so if you run script multiple times it will not overwrite the log):

Notes

You can find the colors you can use by using simple code:

[enum]::GetValues([System.ConsoleColor]) | Foreach-Object {Write-Host $_ -ForegroundColor $_ }

Obviously black is missing 🙂

This post was last modified on May 9, 2018 13:15

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

2 weeks ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

5 months ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

7 months ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

8 months ago

How to Efficiently Remove Comments from Your PowerShell Script

As part of my daily development, I create lots of code that I subsequently comment…

9 months ago

Unlocking PowerShell Magic: Different Approach to Creating ‘Empty’ PSCustomObjects

Today I saw an article from Christian Ritter, "PowerShell: Creating an "empty" PSCustomObject" on X…

9 months ago