Chapter 4. The pipeline: connecting commands

 

In chapter 2, you saw that running commands in PowerShell is basically the same as running commands in any other shell: you type a command name, give it some parameters, and hit Return. What makes PowerShell so special isn’t the way it runs commands, but rather the way it allows multiple commands to be connected to each other in powerful, one-line sequences.

4.1. Connect one command to another: less work for you!

PowerShell connects commands to each other in something called a pipeline. The pipeline is simply a way for one command to pass, or pipe, its output to another command, so that the second command has something to work with.

You’ve already seen this in action when you run something like Dir | More. You’re piping the output of the Dir command into the More command; the More command takes that directory listing and displays it one page at a time. PowerShell takes that same piping concept and extends it to much greater effect. In fact, PowerShell’s use of a pipeline may seem similar, at first, to how Unix and Linux shells work. Don’t be fooled, though. As you’ll come to realize over the next few chapters, PowerShell’s pipeline implementation is much richer and more modern.

4.2. Exporting to a CSV or XML file

Run a simple command. Here are a few suggestions:

  • Get-Process (or Ps)
  • Get-Service (or Gsv)
  • Get-EventLog Security -newest 100

4.3. Piping to a file or printer

4.4. Converting to HTML

4.5. Using cmdlets to kill processes and stop services

4.6. Lab