Scroll Top
Evotec Services sp. z o.o., ul. Drozdów 6, Mikołów, 43-190, Poland

PowerShell – How to format Write-Host with multiple colors

Write-Color

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"

Write-Color

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 🙂

Related Posts

Leave a comment

You must be logged in to post a comment.