Updated March 23, 2023
Introduction to Powershell Write-Host
The write-host cmdlet is used to display the output in the PowerShell console itself. You can customize the displayed output with various font colors and background colors. When the object is passed to the pipeline Write-host does not take it as an object instead, it considers it as a name parameter or value and simply displays output in the console.
[[-Object] <Object>] [-NoNewline] [-Separator <Object>] [-ForegroundColor <ConsoleColor>] [-BackgroundColor <ConsoleColor>] [<CommonParameters>]
- –Object: It is used to display in console output. Generally, the object is a string object, variable or can be operational values.
- –NoNewLine: With this parameter, the output will be continued to the same line, not in the new line.
- –Separator: You can separate words in the bracket with a specified character or word. An explanation is given later in examples.
- –ForegroundColor: You can set the color of the text while displaying output. There is no default color.
The Colors are as below:
- –Background Color: This parameter specifies the background color of the text. There is no default color. The colors are as below.
- <CommonParameters>: This cmdlet supports the common parameters.Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable and OutVariable.
Examples of Powershell Write-Host
Below given are some examples of Powershell write-host:
Example #1: Write-Host with Object
Write-Host "This is a Simple String"
Similarly, you can write the same with passing string to the Pipeline and the output will be the same as above.
"This is a simple string" | Write-Host
Now suppose, we are passing the multiple objects together. Let’s consider Get-Service to the write-host.
Get-Service | Select -First 10 | Write-Host
But when you use Write-Output
Get-Service | Select -First 10 | Write-Output
So, write-host only writes the name of the service, not the entire Get-Service output because Write-Host cmdlet can’t process the entire output as a single object but the single value or string. You can display all default values using the Write-Output Parameter.
Example #2: Write-Host with –NoNewLine
By default, when the write-host command is executed at the end new line parameter is also executed so whatever you write output will be in the next line but using –NoNewLine parameter, the output will be concatenated.
Write-Host "This is first line..." -NoNewline
Write-Host "There is no space"
Example #3: Write-Host with –Separator
With the separator parameter, you can put a specific character or word between each value. For example,
Write-Host (1,2,3,4,5) -Separator "->"
Write-Host ("This","is","string") -Separator ","
You can also separate values with a specific word.
Write-Host ("This","is","string") -Separator " hi "
Separator won’t work with a single value or string.
Write-Host "This is string" -Separator "->"
Example #4: Write-Host with –Foregroundcolor
Sometimes you need to display output text with a specific color. You can use different colors to display text.
Write-Host "This is magenta foreground text" -ForegroundColor Magenta
Write-Host "This is Dark cyan foreground text" -ForegroundColor DarkCyan
Example #5: Write-Host with –Backgroundcolor
You can change the background color of the text with this parameter.
Write-Host "This is white text with Dark Red background"-
Write-Host "Magenta text with green background" -ForegroundColor Magenta -
Write-host can’t capture or store the output, it simply displays the output. To capture the output you need to use the write-output cmdlet but with the latter command, you can’t use the different colors.
For example, in the below command output will not be stored in test.txt and instead, it will be displayed in the PowerShell console.
Write-Host "Simple text" > c:\test.txt
But when you use write-output you can store in a text file.
Write-Output "Simple text" > c:\test.txt
This has been a guide to Powershell Write-Host. Here we discuss the syntax, parameters, and various examples of Powershell Write-Host with appropriate code and output. You may also have a look at the following articles to learn more –