Chapter 19. Input and output

To this point in the book, we’ve primarily been relying on PowerShell’s native ability to output tables and lists. As you start to combine commands into more-complex scripts, you’ll probably want to gain more precise control over what’s displayed. You may also need to prompt a user for input. In this chapter, you’ll learn how to collect that input, and how to display whatever output you might desire.

We want to point out, however, that the contents of this chapter are useful only for scripts that interact with human eyeballs and fingertips. For scripts that run unattended, these aren’t appropriate techniques, because there won’t be a human being around to interact with.

19.1. Prompting for, and displaying, information

The way PowerShell displays and prompts for information depends on how it’s being run. You see, PowerShell is built as a kind of under-the-hood engine.

What you interact with is called a host application. The command-line console you see when running PowerShell.exe is often called the console host. The graphical Power-Shell ISE is usually called the ISE host or the graphical host. Other non-Microsoft applications can host the shell’s engine as well. You interact with the hosting application, and it passes your commands through to the engine. The hosting application displays the results that the engine produces.

19.2. Read-Host

19.3. Write-Host

19.4. Write-Output

19.5. Other ways to write

19.6. Lab

19.7. Further exploration

19.8. Lab answers

sitemap