Write host PowerShell cmdlet used to write something on the console. It is helpful when you need to check any value in the script or need to display a message on the console. Although there are a number of different ways to output messages Write-host is the most commonly used cmdlet in Windows PowerShell.
Syntax of Write-Host Cmdlet
Just write “Write-host” and put object value to display in the host.
Write-Host “Hello World. I am new to PowerShell”
Suppose you have one long script let say 500 to 1000 lines and in that lot of variable have used now if you need to debug that script and check the values of those variables then write-host cmdlet can be used to print the value on console. There are different parameter can be used with the write-host cmdlet.
Below the list of parameters can be used with Write-Host cmdlet and all of these are optional.
Object or its value to display in the host. The default value is none.
Write-host “This is my first message”
As the name suggests it write to the console without adding a new line. No spaces or newlines are inserted between the two or more output strings.
Write-host “This is the first line” –NoNewline “this is the second line”
Output – This is the first line this is the second line
Specifies the forecolor of the text. There is no default value. The acceptable values for this parameter are:
Write-Host "Hello World" -ForegroundColor Green
The “Hello World” text will print in green color on the console. This parameter will help if need to print multiple messages with different colors in a single script.
Specifies the background color of the text. There is no default. The acceptable values for this parameter are:
Write-Host "Hello World" -BackgroundColor Green
The “Hello World” text will print in white and its background color in green on the console.
Specifies a separator string to insert between objects displayed by the host.
Suppose you have string like [1,2,3,4,5,6,7,8,9,10] instead of comma we need a different separate like “+1=”
Write-host [1,2,3,4,5,6,7,8,9,10] –Separator “+1=”
Output – [1+1=2+1=3+1=4+1=5+1=6+1=7+1=8+1=9+1=10]
I hope now you have understood the use of Write-host cmdlet and its parameters.