PowerShell Write-Host

The PowerShell Write-Host cmdlet is used to write the customized output to a host. We can specify the text color by using the -foreground parameter, and by using the -background parameter, we can specify the background color.

Syntax

Parameters

-Object

The -Object parameter is used to display the objects in the host.

-NoNewline

The -NoNewline parameter specifies that no spaces or newlines are inserted between the output strings, and there is no newline which is added after the last output string.

-Separator

The -Separator parameter is used to specify a separator string which is inserted between the objects displayed by the host.

-ForegroundColor

The -ForegroundColor parameter is used to specify the color of the text. In this parameter, there is no default value exists.

Following are the acceptable values for this parameter:

  • Black
  • DarkBlue
  • DarkMagenta
  • DarkYellow
  • Gray
  • DarkGray
  • Blue
  • Green
  • Magenta
  • Yellow
  • White
  • DarkGreen
  • DarkCyan
  • DarkRed
  • Cyan
  • Red

-BackgroundColor

The -BackgroundColor parameter is used to specify the color of the background. There is no default value exists in this parameter. Following are the acceptable values for this parameter:

  • Magenta
  • Yellow
  • White
  • DarkYellow
  • Gray
  • DarkGray
  • Blue
  • Green
  • Cyan
  • Red
  • Black
  • DarkBlue
  • DarkGreen
  • DarkCyan
  • DarkRed
  • DarkMagenta

Examples

Example 1: Write output to the console by including a separator

PowerShell Write-Host

The cmdlet in this example is used to display the numbers from two through twelve. The -Separator parameter is used to multiply the string.

Example 2: Write with different text and background color

PowerShell Write-Host

The cmdlet in this example displays the odd numbers from one through 17. It uses the -ForegroundColor parameter to output ‘Red’ text and the -BackgroundColor parameter to display a ‘black’ background.