Multiple Foreground Colors in PowerShell in One Command
Write-HostCmdlet to Output Multiple Foreground Colors in PowerShell
Write-ColorCmdlet 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.
Write-Host Cmdlet to Output Multiple Foreground Colors in PowerShell
Write-Host cmdlet has two parameters,
-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
-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.
Write-Color Cmdlet to Output Multiple Foreground Colors With One Command in PowerShell
Write-Color cmdlet is available in the PowerShell module. You can install it using the command below.
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
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
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.