Welcome to this addition of the PowerShell ABC's where you'll find 26 posts detailing a component of the PowerShell scripting language, one letter at a time. Today's letter is the letter "O" and for this letter I'll discuss the Output system.
PowerShell, like most other scripting languages, wouldn't be much use without an input and output system for retrieving or sending data. PowerShell has various output systems.
A pipeline is a series of commands separated by the pipe operator "|". Each command in the pipeline receives an object from the previous command, performs some operation on that object, and then passes it along to the next command in the pipeline. One of the distinguishing features of Pipelines in PowerShell vs. other shells is that it uses objects through the pipeline as opposed to strings. This enables the commands in the pipeline chain to have full access to the object as well as all of it's attributes and members.
Consider the example of a simple directory listing with the Get-ChildItem Cmdlet.
By having access to the object, I was able to extract the CreationTime which I wouldn't have been able to do with a traditional shell that just passed the text representation of the output of a directory listing.
Now that you've learned how to pass the output of a command to another command, you'll likely want to be able to make that output available to you. This can be done by writing the output to the console, a file, a variable, a printer, or some other form of persistent storage. The main output mechanisms can be found in the Out-* Cmdlets listed below:
Out-Default - Send the output to the default formatter and the default output cmdlet.
Out-String - Sends objects to the host as a series of strings.
PowerShell provides a mechanism in which the PowerShell runtime can be embedded inside other applications. The host you are probably most familiar with is the Interactive PowerShell console but the host could just as easily be your favorite GUI application that "hosts" the PowerShell runtime.
The Out-Host Cmdlet, sends the output to the default host but you may want more control with the format of your output. This can be accomplished with the the following Cmdlets.
Write-Output - Sends the specified objects to the next command in the pipeline.
Note that some of these Cmdlet's will not display anything to the console by default. The Write-Verbose and Write-Debug Cmdlet's require you to set the $VerbosePreference and $DebugPreference variables to see their output.
No shell language would be complete without allowing for easy input/output redirection. PowerShell has the Out-File Cmdlet that covers the outbound redirections, but the following traditional operators have been added that are aliases to the various formats of the Out-File Cmdlet.