PowerShell: How to Use Write-Host with Specific Colors


You can use the Write-Host cmdlet in PowerShell to output text to the console.

To control the text color used with Write-Host, you can use the following methods:

Method 1: Specify One Color to Use with Write-Host

Write-Host "This text is in red" -f red

Method 2: Specify Multiple Colors to Use with Write-Host

Write-Host "I am red " -f red -nonewline; Write-Host "I am blue " -f blue;

The following examples show how to use each method in practice.

Example 1: Specify One Color to Use with Write-Host

Suppose that you would like to use the Write-Host cmdlet in PowerShell to display the text “Hey there everyone” to the console.

Suppose that we use the following syntax to do so:

Write-Host "Hey there everyone"

The following screenshot shows how to use this syntax in practice:

By default, the font color of the text is grey.

To instead display the text in a red font, we can use the following syntax:

Write-Host "Hey there everyone" -f red

The following screenshot shows how to use this syntax in practice:

PowerShell use Write-Host with specific color

Notice that the text is now displayed using a red font, just as we specified.

Example 2: Specify Multiple Colors to Use with Write-Host

Suppose that you would like to use the Write-Host cmdlet to display a portion of the text in one color and another portion of the text in another color.

You can use the following syntax to do so:

Write-Host "I am red " -f red -nonewline; Write-Host "I am blue " -f blue;

The following screenshot shows how to use this syntax in practice:

PowerShell Write-Host with multiple colors

Notice that the first portion of the text is shown using red font while the latter portion of the text is shown using blue font.

Bonus: Display List of All Possible Colors in PowerShell

PowerShell allows you to specify 16 different colors to use in the console.

You can use the following syntax to display this complete list of colors:

[Enum]::GetValues([System.ConsoleColor]) | foreach { Write-Host "$_" -ForegroundColor $_}

The following screenshot shows how to use this syntax in practice:

PowerShell display list of colors

Each of the 16 colors are shown in a list.

Note that the first color in the list is “black” which can’t be seen because the background color of the console is also black.

Note: You can find the complete documentation for the Write-Host cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Replace Every Occurrence of String in File
PowerShell: How to Replace Special Characters in String
PowerShell: How to Replace Text in String

Leave a Reply