Chapter 17: Input and output
The introduction explains why scripts may need user input and more control over displayed output, while limiting these techniques to scripts that interact with people.
After this chapter you will be able to
- Human-facing input and output. Explain why a script that interacts with a person may need to prompt for input and control what it displays, and why those techniques are inappropriate for an unattended script.
- Host and engine roles. Explain how a PowerShell host application relates to the engine and why the host affects prompting and display. Contrast an interactive host with the unattended Azure Functions host.
- Read-Host input and result. Explain that Read-Host displays a text prompt, collects what the user types, and returns that text as its result, which can be captured in a variable. Describe the colon appended to the prompt.
- Write-Host output behavior. Explain that Write-Host displays text directly in the hosting application without placing it in the pipeline, and that it also writes a record to the information stream.
- Choosing screen message commands. Distinguish the limited use recommended for Write-Host from cases better served by formatting tools or more specific Write cmdlets, including suppressible verbose messages.
- Write-Output display path. Trace how an object sent by Write-Output reaches the screen when no other pipeline command intervenes.
- Pipeline filtering versus direct display. Explain why filtering can prevent Write-Output’s Hello from appearing, while replacing it with Write-Host still shows Hello.
- Alternative output and preferences. Explain that these output commands do not write to the pipeline like Write-Output and that their associated configuration variables control whether messages appear.
What it builds on
- Storing values in variables (chapter 16) · Storing a value in a variable
- Connecting one command to another: Less work for you (chapter 6) · Assumes familiarity with connecting commands through the pipeline.
- Asking for help (chapter 3) · Assumes familiarity with using Get-Help to inspect command parameters.
- Formatting tables (chapter 11) · Assumes familiarity with PowerShell's table-formatting commands.
- Going out: To a file or to the host (chapter 11) · How pipeline objects can be sent to the host for display.
- Filtering objects out of the pipeline (chapter 12) · Filtering objects out of the pipeline with Where-Object.
Section by section
Introduction
The introduction explains why scripts may need user input and more control over displayed output, while limiting these techniques to scripts that interact with people.
- Human-facing input and output: Explain why a script that interacts with a person may need to prompt for input and control what it displays, and why those techniques are inappropriate for an unattended script.
Why it matters: Whether someone will be there to respond determines whether interactive input and output suit a script.
Key terms: prompt a user for input, scripts that run unattended
17.1 Prompting for, and displaying, information
This section explains why PowerShell’s input and output can behave differently depending on the application hosting its engine.
- Host and engine roles: Explain how a PowerShell host application relates to the engine and why the host affects prompting and display. Contrast an interactive host with the unattended Azure Functions host.
Why it matters: The host explains why the same command may interact with you differently in a terminal, Visual Studio Code, or Azure Functions.
Key terms: PowerShell engine, host application, console host, integrated host
17.2 Read-Host
This section explains how Read-Host prompts a user for text and how to keep the response in a variable.
- Read-Host input and result: Explain that Read-Host displays a text prompt, collects what the user types, and returns that text as its result, which can be captured in a variable. Describe the colon appended to the prompt.
Why it matters: It lets you ask for a value, such as a computer name, and use the response in later commands.
Key terms: Read-Host, text prompt, variable
17.3 Write-Host
Explains how Write-Host displays text, why its pipeline behavior matters, and when another Write cmdlet is a better choice.
- Write-Host output behavior: Explain that Write-Host displays text directly in the hosting application without placing it in the pipeline, and that it also writes a record to the information stream.
Why it matters: Seeing text on screen does not mean Write-Host passed that text to the next pipeline command. - Colored host text limitations: Describe how Write-Host can display colored text and why a command should not depend on those colors appearing.
Why it matters: A colored message may look different—or lose its color—in another PowerShell host. - Choosing screen message commands: Distinguish the limited use recommended for Write-Host from cases better served by formatting tools or more specific Write cmdlets, including suppressible verbose messages.
Why it matters: Choosing the appropriate output command keeps messages consistent with PowerShell’s conventions and lets verbose messages be suppressed.
Key terms: Write-Host, pipeline, information stream, hosting application, -ForegroundColor, -BackgroundColor, Write-Verbose, Verbose stream
17.4 Write-Output
This section explains how Write-Output sends objects through the pipeline rather than directly to the screen, and why that difference affects what gets displayed.
- Write-Output display path: Trace how an object sent by Write-Output reaches the screen when no other pipeline command intervenes.
Why it matters: Seeing text on the screen does not mean Write-Output wrote directly to it. - Pipeline filtering versus direct display: Explain why filtering can prevent Write-Output’s Hello from appearing, while replacing it with Write-Host still shows Hello.
Why it matters: The route an object takes determines whether later pipeline commands can act on it. - Implicit Write-Output behavior: Describe when the shell uses Write-Output behind the scenes.
Why it matters: It explains why pipeline output may be familiar even if you have not explicitly typed Write-Output.
Key terms: Write-Output, pipeline, Out-Default, Out-Host, formatting system, Write-Host, Where-Object, Length, default cmdlet
17.5 Other ways to write
This section explains how PowerShell’s alternative output commands differ from pipeline output, how their messages can be suppressed, and why their display location may vary.
- Alternative output and preferences: Explain that these output commands do not write to the pipeline like Write-Output and that their associated configuration variables control whether messages appear.
Why it matters: It helps you understand why an output command may produce no visible message even when it runs. - Information and error streams: Distinguish Write-Host’s use of the information stream from Write-Error’s use of the error stream.
Why it matters: It clarifies that messages shown by different writing commands need not take the same route. - Host-dependent output location: Explain why output from these cmdlets may appear somewhere other than a terminal in some hosting applications.
Why it matters: It reminds you to look in the host’s logging location when a message does not appear in a terminal.
Key terms: Write-Output, Continue, SilentlyContinue, configuration variable, Write-Host, Write-Information, information stream, Write-Error, error stream, hosting applications
17.6 Lab
17.7 Lab answers
17.8 Further exploration
Encourages readers to practice the chapter’s input and output cmdlets and keep syntax reminders for future use.
Where this is used later
- Adding the warm and fuzzies with verbose output (chapter 20) · Motivates using verbose output for optional status messages.
- Process block (chapter 22) · Writing output with Write-Output.
- Output everything (chapter 25) · PowerShell’s different ways to write output
Concepts explained in this chapter
- Write-HostWrite-Host is a PowerShell cmdlet that writes text to the host application's screen and to the information stream.
- information streamThe information stream is a PowerShell output channel for informational messages that can be recorded or consumed separately from ordinary pipeline output.
- Write-VerboseWrite-Verbose is a PowerShell cmdlet that writes diagnostic or progress messages to the verbose stream.
- PowerShell hostA PowerShell host is an interface environment or application that receives PowerShell output and provides interaction with a PowerShell session.
- cmdletA cmdlet is a specialized PowerShell command that performs a focused operation and is typically implemented as a .NET class.
- help fileA cmdlet help file is a documentation file that describes a PowerShell cmdlet, including its syntax, parameters, and behavior.
- expressionAn expression is a PowerShell construct that evaluates to a value.
- -Syntax-Syntax is a Get-Command parameter that displays the syntax of a command.
- commandA command is an executable instruction that a shell can invoke to perform an operation.
- Where-ObjectWhere-Object is a PowerShell cmdlet that filters objects in the pipeline according to specified criteria.
- comparisonA comparison is an expression that evaluates a relationship between values and produces a Boolean result.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
