Edit

AvoidUsingWriteHost

Severity Level: Warning

Description

This rule detects usage of Write-Host in functions that don't use the Show verb. Write-Host is designed to produce display-only output in the host, like printing colored text or prompting users for input with Read-Host. It uses the ToString() method to write output, with results depending on the PowerShell host program.

Since Write-Host doesn't send output to the pipeline, you'll need Write-Output or implicit output to pass data down the pipeline.

Avoid using Write-Host in functions unless they use the Show verb, which explicitly means display information to the user. This rule doesn't apply to functions with the Show verb.

Replace Write-Host with Write-Output or Write-Verbose based on your intention. Use Write-Verbose for logging and Write-Output for returning objects.

Example

Noncompliant

function Get-MeaningOfLife
{
    Write-Host 'Computing the answer to the ultimate question of life, the universe and everything'
    Write-Host 42
}

Compliant

Use Write-Verbose for informational messages. The user can decide whether to see the message by providing the Verbose parameter.

function Get-MeaningOfLife
{
    [CmdletBinding()]Param() # makes it possible to support Verbose output

    Write-Verbose 'Computing the answer to the ultimate question of life, the universe and everything'
    Write-Output 42
}

function Show-Something
{
    Write-Host 'Show something on screen'
}