Silencing Write-Host Statements

by Jul 15, 2020

Write-Host is an extremely useful cmdlet to output information to a user because this output cannot be discarded:

function Invoke-Test
{
  "Regular Output"
  Write-Host "You always see me!"
}

# both show
Invoke-Test

# Write-Host still shows
$result = Invoke-Test

Beginning in PowerShell 5, there was a silent change in the engine though. Output emitted from Write-Host is now also controlled by the stream system, and Write-Host shares the new information stream with Write-Information.

If you wanted to hide messages emitted by Write-Host, simply redirect stream #6 to $null:

 
PS> $result = Invoke-Test 6>$null
 

For more information on streams and redirection, visit https://powershell.one/code/9.html.


Twitter This Tip! ReTweet this Tip!