Chapter 21. Input, Output, and Error Handling in PowerShell

It's important to learn early in your PowerShell career the range of options you have for output. Output can go into a variable, it can be sent to a file, it can be piped into another command, or it can be left to drop out to the console. One of the first lessons in this area to learn is that you don't have to do anything to get output. Writing in PowerShell gives you access to many facilities which control output and handle XML, HTML, and CSV formats with minimal effort. In fact, one of the reasons that people take up PowerShell in the first place is the small amount of work needed to produce well-formatted output.

This chapter is going to look at some of the inputs and outputs and some of the traps that might be there for people coming to PowerShell for other environments.

Output to the Console

Arguably the most famous piece of computer program is the one that appears at the start of Kernighan and Ritchie's The C Programming Language, which contains the line:

printf("Hello, world!\n\r");

Ever since that book appeared, people learning any new language have started with something to output some basic text on whatever console device they are using, be that a teletype printer, a terminal screen, or a text window in Windows. PowerShell ...

Get Microsoft® PowerShell, VBScript and JScript® Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.