Tuesday, 30 August 2016

Powershell - Write Output in Console

There are multiple ways to write output to powershell console, you can simply put quotation marks in any string to display on the screen and you can also use the powershell cmdlets Write-Host and Write-Output.
"Hello World1"
Write-Host "Hello world2"
Write-Output "Hello World3"
The Write-Host is the best choice in many cases, it is the cmdlet designed specifically to display output only on powershell console screen. You can display the output with background color and foreground color by using Write-Host.
Write-Host "Hello world" -BackgroundColor "Green" -ForegroundColor "Black"
Write-Output cmdlet should be used when you want to send data into the pipe line, but not necessarily want to display it on screen. Because, you can't pipe the output when you use Write-Host.
function Test-Output {
    Write-Output "Hello World"

function Receive-Output {
    process { Write-Host $_ -ForegroundColor Yellow }

#Output piped to another function, not displayed in first.
Test-Output | Receive-Output
You can get more detail about the cmdlets Write-Host and Write-Output from this article : https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/17/writing-output-with-powershell/


No comments:

Post a Comment