liveBook · Manning

Write-Host

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Write-Host is a PowerShell cmdlet that writes content directly to the host interface and, in PowerShell v5 and later, also routes it through the information stream.

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

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Explains host behavior, PowerShell 5 stream integration, practical exceptions, and alternatives, but stops short of complete cmdlet syntax.
Examples
Uses colored status messages, captured InformationRecord objects, tagged messages, and a contrasting structured-object pipeline example.
What it assumes
Assumes familiarity with PowerShell commands and streams while contrasting reusable objects with human-oriented output.
What you can do after
Evaluate/understand when to replace Write-Host with Write-Information, Write-Progress, or structured pipeline output.
How it connects
Connects Write-Host with Write-Information, information streams, pipelines, structured objects, and Write-Progress.

Also explained in

Compare how 2 books explain Write-Host →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.