Write-Host
Write-Host [[-Object] <Object>] [-BackgroundColor {Black | DarkBlue | DarkGreen | DarkCyan | DarkRed | DarkMagenta| DarkYellow | Gray | DarkGray | Blue | Green | Cyan | Red | Magenta | Yellow | White}] [-ForegroundColor {Black |DarkBlue | DarkGreen | DarkCyan | DarkRed | DarkMagenta | DarkYellow | Gray | DarkGray | Blue | Green | Cyan | Red| Magenta | Yellow | White}] [-NoNewline] [-Separator <Object>] [<CommonParameters>]
The Write-Host cmdlet customizes output. You can specify the color of text by using the ForegroundColor parameter, and you can specify the background color by using the BackgroundColor parameter. The Separator parameter lets you specify a string to use to separate displayed objects. The particular result depends on the program that is hosting Windows PowerShell.
Parameters
-BackgroundColor <ConsoleColor>
- Default value is None
- Accepts pipeline input False
Specifies the background color. There is no default. The acceptable values for this parameter are:
- Black
- DarkBlue
- DarkGreen
- DarkCyan
- DarkRed
- DarkMagenta
- DarkYellow
- Gray
- DarkGray
- Blue
- Green
- Cyan
- Red
- Magenta
- Yellow
- White
-ForegroundColor <ConsoleColor>
- Default value is None
- Accepts pipeline input False
Specifies the text color. There is no default. The acceptable values for this parameter are:
- Black
- DarkBlue
- DarkGreen
- DarkCyan
- DarkRed
- DarkMagenta
- DarkYellow
- Gray
- DarkGray
- Blue
- Green
- Cyan
- Red
- Magenta
- Yellow
- White
-NoNewline [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Specifies that the content displayed in the console does not end with a newline character.
-Object <Object>
- Default value is None
- Accepts pipeline input ByValue
Specifies objects to display in the console.
-Separator <Object>
- Default value is None
- Accepts pipeline input False
Specifies a separator string to the output between objects displayed on the console.
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.
Inputs
System.ObjectOutputs
NoneExamples
-
Write to the console without adding a new line:
PS C:\> Write-Host "no newline test " -NoNewline no newline test PS C:\>
This command displays the input to the console, but because of the NoNewline parameter, the output is followed directly by the prompt.
-
Write to the console and include a separator:
PS C:\> Write-Host (2,4,6,8,10,12) -Separator ", +2= " 2, +2= 4, +2= 6, +2= 8, +2= 10, +2= 12
This command displays the even numbers from 2 through 12. The Separator parameter is used to add the string ", +2= (comma, space, +, 2, =, space)".
-
Write with different text and background colors:
PS C:\> Write-Host (2,4,6,8,10,12) -Separator ", -> " -ForegroundColor DarkGreen -BackgroundColor white
This command displays the even numbers from 2 through 12. It uses the ForegroundColor parameter to output dark green text and the BackgroundColor parameter to display a white background.
-
Write with different text and background colors:
PS C:\> Write-Host "Red on white text." -ForegroundColor red -BackgroundColor white Red on white text.
This command displays the string "Red on white text." The text is red, as defined by the ForegroundColor parameter. The background is white, as defined by the BackgroundColor parameter.
Additional Notes
This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.
PowerShell Commands