Multiple Foreground Colors in PowerShell in One Command
-
Use the
Write-Host
Cmdlet to Output Multiple Foreground Colors in PowerShell -
Use the
Write-Color
Cmdlet to Output Multiple Foreground Colors With One Command in PowerShell
There are multiple cmdlets in PowerShell to display output on the console. The Write-Host
is a popular cmdlet that allows you to print the colored text in the output.
With a single Write-Host
command, you can print the text in only one color. You have to use multiple Write-Host
commands to display multiple colors.
This tutorial will introduce different methods to output text with multiple foreground colors in PowerShell.
Use the Write-Host
Cmdlet to Output Multiple Foreground Colors in PowerShell
The Write-Host
cmdlet has two parameters, -ForegroundColor
and -BackgroundColor
, for printing the colored text. The -ForegroundColor
parameter changes the text color, whereas the -BackgroundColor
changes the background color.
The accepted color values are:
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White
The following command changes the text color to green.
Write-Host "Hello World" -ForegroundColor Green
The parameter accepts only one value, so you cannot specify multiple colors to display. You will need to use several Write-Host
commands for multiple colors.
This command prints the string in two different colors: green and red.
Write-Host "Hello " -ForegroundColor Green -NoNewline; Write-Host "World" -ForegroundColor Red
Output:
The -NoNewline
parameter allows you to print multiple text strings in a single line. The specified string will be printed on the new line if it is not used.
Use the Write-Color
Cmdlet to Output Multiple Foreground Colors With One Command in PowerShell
The Write-Color
cmdlet is available in the PowerShell module. You can install it using the command below.
Install-Module PSWriteColor
The Write-Color
allows you to print the output with multiple colors in a very simple way. The following command changes the string Hello World
to the specified color in the output.
Write-Color -Text "Hello World" -Color Green
To display text in multiple colors, you must separate the strings and colors with a comma ,
. For example, this command shows two strings in two different colors on the same line:
Write-Color -Text "Hello ", "World" -Color Green, Yellow
Output:
Let’s see another example to display multiple foreground colors with Write-Color
in PowerShell.
Write-Color -Text "Some text ",
"in different ",
"colors as",
"you can see. " -Color Green, Yellow, Red, Blue
Output:
The Write-Color
is short and easy to use for printing different colored text. And now you should know how to have multiple text colors on the same line in PowerShell output.