Write-Host
Writes customized output to a host.
语法
Default (默认值)
Write-Host
[[-Object] <Object>]
[-NoNewline]
[-Separator <Object>]
[-ForegroundColor <ConsoleColor>]
[-BackgroundColor <ConsoleColor>]
[<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 PowerShell.
Note
Starting in Windows PowerShell 5.0, Write-Host
is a wrapper for Write-Information
This allows you to use Write-Host
to emit output to the information stream.
This enables the capture or suppression of data written using Write-Host
while preserving backwards compatibility.
The $InformationPreference
preference variable and InformationAction
common parameter do not affect Write-Host
messages.
The exception to this rule is -InformationAction Ignore
, which effectively suppresses Write-Host
output. (see "Example 5")
示例
Example 1: Write to the console without adding a new line
Write-Host "no newline test " -NoNewline
Write-Host "second string"
no newline test second string
This command displays the string 'no newline test' with the NoNewline
parameter.
A second string is written, but it ends up on the same line as the first due to the absence of a newline separating the strings.
Example 2: Write to the console and include a separator
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 two through twelve.
The Separator parameter is used to add the string , +2= (comma, space, +, 2, =, space)
.
Example 3: Write with different text and background colors
Write-Host (2,4,6,8,10,12) -Separator ", -> " -ForegroundColor DarkGreen -BackgroundColor White
2, -> 4, -> 6, -> 8, -> 10, -> 12
This command displays the even numbers from two through twelve.
It uses the ForegroundColor
parameter to output 'dark green' text and the BackgroundColor
parameter to display a 'white' background.
Example 4: Write with different text and background colors
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.
Example 5: Suppress output from Write-Host
# The following two statements can be used to effectively suppress output from Write-Host
Write-Host "I won't print" -InformationAction Ignore
Write-Host "I won't print" 6>$null
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.
参数
-BackgroundColor
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
参数属性
类型: | ConsoleColor |
默认值: | None |
接受的值: | Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White |
支持通配符: | False |
不显示: | False |
参数集
(All)
Position: | Named |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | False |
来自剩余参数的值: | False |
-ForegroundColor
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
参数属性
类型: | ConsoleColor |
默认值: | None |
接受的值: | Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White |
支持通配符: | False |
不显示: | False |
参数集
(All)
Position: | Named |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | False |
来自剩余参数的值: | False |
-NoNewline
The string representations of the input objects are concatenated to form the output. No spaces or newlines are inserted between the output strings. No newline is added after the last output string.
参数属性
类型: | SwitchParameter |
默认值: | None |
支持通配符: | False |
不显示: | False |
参数集
(All)
Position: | Named |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | False |
来自剩余参数的值: | False |
-Object
Objects to display in the host.
参数属性
类型: | Object |
默认值: | None |
支持通配符: | False |
不显示: | False |
参数集
(All)
Position: | 0 |
必需: | False |
来自管道的值: | True |
来自管道的值(按属性名称): | False |
来自剩余参数的值: | False |
-Separator
Specifies a separator string to insert between objects displayed by the host.
参数属性
类型: | Object |
默认值: | None |
支持通配符: | False |
不显示: | False |
参数集
(All)
Position: | Named |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | False |
来自剩余参数的值: | False |
CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
输入
Object
You can pipe objects to be written to the host.
输出
None
Write-Host
sends the objects to the host.
It does not return any objects.
However, the host might display the objects that Write-Host
sends to it.