One of the most welcome new features that Microsoft has built into PowerShell 7.2 is support for ANSI escape sequences. If you aren’t familiar with ANSI escape sequences, they are an industry standard format for decorative text and were first used with Xterm. These sequences allow you to create colored text and to add effects such as bold, italic, and underline.
This isn’t to say that it was previously impossible to create colorful text with PowerShell. It was actually really easy to do. However, PowerShell’s newfound support for ANSI escape sequences allow you to color PowerShell’s output in a more standardized way. It also gives you a few additional formatting options that (to the best of my knowledge) were not previously supported by PowerShell.
The Old Way of Doing Things
As previously mentioned, PowerShell has long supported the use of color. The most commonly used technique for generating colorful output involved appending a foreground color and/or a background color to the write-host cmdlet. If for example, you wanted to display white text on a red background, then you could do so by using a command like this:
Write-Host “Hello World” -ForegroundColor White -BackgroundColor Red
You can see what this looks like in the figure below. What’s more important is that the figure confirms that in spite of PowerShell’s new support for ANSI escape sequences, the old method of generating colored output still works just as it always has.
This is the legacy method for formatting output text in PowerShell.
The ANSI Method
The ANSI escape sequence method works a little bit differently than what I just showed you, although as you will see, there are some similarities. Microsoft implemented support for ANSI escape sequences through the use of a variable named $PSStyle.
In its simplest form, there are three pieces of information that you need to supply when using this variable. First, you have to supply the name of the variable itself. Second, you have to provide the name of the item that you are trying to affect (such as the background). Third, you must specify the color that you want to use. These three items are separated by periods. So if for example, you want to set the background to red, then you would enter: $PSStyle.Background.Red. Incidentally, the red color that is produced by this command is kind of dull. If you prefer a more vibrant color then just change red to BrightRed. You can see what this looks like in the figure below.
You can use the $PSStyle variable to set the background color.
Of course you can do a lot more with the $PSStyle variable than just setting the PowerShell background color. I am not going to go into everything that you can do with $PSStyle. Some of the use cases are really unique and are way beyond the scope of this article, though I hope to cover some of them in the future. For example, there are $PSStyle properties that can be used to format progress bars or file objects. Some of the more useful properties include:
It is worth noting that there are Off properties for most of these properties, with the exception of Foreground and Background. Let’s suppose for instance that you use the Bold property to bold some text. Once that text has been bolded, you could use the BoldOff property to prevent subsequent text from being bolded. There is also a Reset property that you can use to turn off all text decorations rather than you having to deal with turning off each decoration individually.
So let’s suppose for a moment that that I wanted to output the phrase “This is how you bold text in PowerShell” and that I wanted to bold the word “bold.” Here is how you could do it:
“$($PSStyle.Reset)This is how you $($PSStyle.bold)bold$($PSStyle.BoldOff) text in PowerShell”
Technically, I didn’t have to start out by using the Reset property, but I wanted to show you how Reset could be used to cancel out any other styles that are in effect. That means that the words “this is how you” are displayed in normal text. The Bold property is applied to the word “bold” and then the BoldOff property causes the remaining text to be displayed in normal type. You can see what this looks like below. Incidentally, you will notice that I did not have to specify a color along with these properties because not every property requires a color.
This is how you bold a word in a sentence.
So what if you wanted to incorporate these types of ANSI escape sequences into a normal Write-Host command? You could use them in almost exactly the same way that I did in the previous example, but there is a cleaner method that I prefer using.
Imagine for a moment that I am building a PowerShell script and that I want all of my output text to appear as white text on a red background. The first thing that I would generally do in that type of situation is to map the style to a variable. Here is an example:
$Style = “$($PSStyle.Background.BrightRed)$($PSStyle.Foreground.BrightWhite)”
With the variable defined, you can incorporate it into a Write-Host statement. Here is an example of what such a statement might look like:
Write-Host $Style”Hello World”
I have defined a style and used it in conjunction with the Write-Host cmdlet.
One interesting thing to consider is that you aren’t limited to keeping things as simple as I have. You could easily create a number of different styles. These styles could incorporate different color schemes, bold text, italics, and what ever other decorations you wish to include. You could even create a style library within an entirely separate PowerShell script and then reference that library from all of the other scripts that you write. That way, you can define the styles once and then use them throughout all of your PowerShell scripts.