Write-Host writes content directly to the host interface. In PowerShell 5 and later, it also acts as a conduit for the information stream, but reusable tools should usually emit structured objects instead. section 12.2 section 18.9
Choose the output mechanism according to the tool’s purpose:
- Avoid Write-Host for ordinary tool output because host-bound text cannot be reliably reused, redirected, or transformed like pipeline objects. section 12.2
- Use Write-Information when messages belong in the Information stream; it supports actions, variables, tags, searching, and later storage. section 13.4.1 section 18.9
- Use Write-Host for deliberately human-interactive controllers or commands using the Show verb, such as a menu. section 18.9
- Use Write-Progress instead when the goal is reporting activity during lengthy or complex processing. section 18.9
Listing 13.3 Revised function
Function Test-Me2 {
[cmdletbinding()]
Param()
Write-Host "Starting $($MyInvocation.MyCommand) " -foreground green
Write-Host "PSVersion = $($PSVersionTable.PSVersion)" -foreground green
Write-Host "OS = $((Get-CimInstance Win32_operatingsystem).Caption)"
-foreground green
Write-Verbose "Getting top 5 processes by WorkingSet"
Get-Process | sort WS -Descending | select -first 5 -OutVariable s
Write-Host ($s[0] | Out-String) -foreground green
Write-Host "Ending $($MyInvocation.MyCommand) " -foreground green
}
Compare a revised function that uses colored Write-Host messages with an Information-stream implementation before choosing the more reusable design. section 13.4.1

