> ## Documentation Index
> Fetch the complete documentation index at: https://tally.wharflab.com/llms.txt
> Use this file to discover all available pages before exploring further.

# powershell/PSAvoidUsingWriteHost

> Avoid Using Write-Host

`powershell/PSAvoidUsingWriteHost` is a PSScriptAnalyzer diagnostic emitted by tally for PowerShell snippets embedded in Dockerfiles.

| Property | Value             |
| -------- | ----------------- |
| Severity | Warning           |
| Category | PSScriptAnalyzer  |
| Default  | Disabled in tally |
| Auto-fix | No                |

<Note>
  tally disables this rule by default for Dockerfile `RUN` snippets because there is no downstream
  pipeline or interactive host inside a one-shot container build — the script-reusability concerns
  this rule targets do not apply. Re-enable it with
  `include = ["powershell/PSAvoidUsingWriteHost"]` or by setting
  `rules.powershell.PSAvoidUsingWriteHost.severity = "warning"` in `.tally.toml`.
</Note>

## Description

The primary purpose of the `Write-Host` cmdlet is to produce display-only output in the host. For
example: printing colored text or prompting the user for input when combined with `Read-Host`.
`Write-Host` uses the `ToString()` method to write the output. The particular result depends on the
program that's hosting PowerShell. The output from `Write-Host` isn't sent to the pipeline. To
output data to the pipeline, use `Write-Output` or implicit output.

The use of `Write-Host` in a function is discouraged unless the function uses the `Show` verb. The
`Show` verb explicitly means *display information to the user*. This rule doesn't apply to functions
with the `Show` verb.

## How

Replace `Write-Host` with `Write-Output` or `Write-Verbose` depending on whether the intention is
logging or returning one or more objects.

## Examples

### Problematic code

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

### Correct code

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

```powershell theme={null}
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'
}
```

## More information

[Write-Host](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/write-host)

## Source

This rule documentation is adapted from Microsoft's PSScriptAnalyzer documentation for
[AvoidUsingWriteHost](https://github.com/MicrosoftDocs/PowerShell-Docs-Modules/blob/main/reference/docs-conceptual/PSScriptAnalyzer/Rules/AvoidUsingWriteHost.md),
licensed under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).
