Directing and Formatting Windows PowerShell 1.0 Output

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Windows PowerShell 1.0 String Quoting and Escape SequencesUnderstanding and Creating Windows PowerShell 1.0 Variables

Purchase and download the full PDF version of this PowerShell eBook for only $8.99

Buy eBook

It will be extremely rare that a Windows PowerShell interactive shell session or script execution does not need to display output of one form or another. Fortunately, in recognition of this fact, the creators of PowerShell provided not just a number of different ways to output information to users, files and printers, but also some useful generic formatting commands. Each of these outputters and formatters are actually cmdlets and will all be covered in this chapter of Windows PowerShell 1.0 Essentials.

The Default Output

By default (in other words unless a specific output destination is specified) the output from any given command is displayed in the PowerShell console window. The cmdlet used for this purpose is called Out-Default. In practice, output directed to Out-Default is actually handed off to the Out-Host cmdlet.

The Out-Host Cmdlet

The PowerShell Out-Host cmdlet directs output to the application which is hosting the PowerShell session. In most cases this is the PowerShell console or command prompt (cmd.exe) but could equally be any application which is capable of launching and hosting the PowerShell.

The Out-Printer Cmdlet

The Out-Printer cmdlet directs output to a printer. If no arguments are provided, the output is sent to the printer which is designated as the default on the host Windows system. Alternatively, the -name parameter may be used together with the name of the destination printer as an argument. For example, the following command sends the contents of the file named mydata.txt to the printer named "HP Deskjet 5800":

PS C:\Windows\Users\Administrator > get-content mydata.txt | out-print -name "HP Deskjet 5800"

The Out-File Cmdlet

As the name suggests, the Out-File cmdlet writes output to a file. PowerShell supports a number of different encoding formats (unicode, utf7, utf8, utf32, ascii, bigendianunicode and oem), one of which may be specified as an argument to the -encoding parameter. For example, the following command reads a line of input from the user and subsequently writes it to a file called mydata.txt located in the current directory using ASCII encoding:

PS C:\Users\Administrator> read-host | out-file -encoding ASCII mydata.txt

The Out-Null Cmdlet

The purpose of the Out-Null cmdlet is quite simply to discard output. For those familiar with UNIX or Linux systems this is analogous to directing output to /dev/null.

This cmdlet can be useful, for example, when it is necessary to hide unwanted output from the user when a script is executing.

The Out-String Cmdlet

By default, the Out-String cmdlet concatenates all the lines of output from the preceding command in the pipe into a single raw string (in other words not as a string object) and passes it to the next cmdlet. If the lines of output are to be packaged as separate strings the command should be used with the -stream parameter.

Whilst of little use within the context of the PowerShell environment, this cmdlet is provided in order to facilitate interaction with external entities which can only handle raw strings and would not be able to deal with a .Net string if one was sent its way.

Formatting PowerShell Output

So far in this chapter we have explored techniques to direct output generated from Windows PowerShell scripts but have left PowerShell to decide how to format the output based on the data's type. In practice, however, it is unlikely that the default format will always be the desired format. Consequently, PowerShell provides a number of formatting cmdlets through which output may be piped using the '|' character to change the way the information is presented to the user.

The first of these format cmdlets is the format-table command (also available via the ft alias). This cmdlet displays the output data as a series of columns. This is actually the default format option for output from the get-childitem command which displays the contents of a directory. For example:

PS C:\Users\Administrator> get-childitem | ft

    Directory: Microsoft.PowerShell.Core\FileSystem::C:\Users\Administrator

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d-r--         11/7/2008   4:26 PM            Contacts
d-r--         11/7/2008   4:26 PM            Desktop
d-r--         11/7/2008   4:26 PM            Documents
d-r--         11/7/2008   4:26 PM            Downloads

By default, format-table takes a best guess approach to deciding how wide the table fields should be. Unfortunately, what may be the correct width for the first few rows of data may prove to be insufficient for later rows. An alternative is to use the -autosize switch parameter which will process all the lines of data to find the optimal field widths before the data is displayed. The downside to this approach is that for large volumes of data there may be a significant delay before the information is displayed.

Another formatting option is provided by the format-list (aliased as fl) cmdlet. This command displays data in a list format. For example, the same get-childitems data appears as follows when piped through format-list:

 PS C:\Users\Administrator> get-childitem | fl

    Directory: Microsoft.PowerShell.Core\FileSystem::C:\Users\Administrator

Name           : Contacts
CreationTime   : 11/7/2008 4:26:15 PM
LastWriteTime  : 11/7/2008 4:26:15 PM
LastAccessTime : 11/7/2008 4:26:15 PM

Name           : Desktop
CreationTime   : 11/7/2008 4:26:05 PM
LastWriteTime  : 11/7/2008 4:26:15 PM
LastAccessTime : 11/7/2008 4:26:15 PM

Name           : Documents
CreationTime   : 11/7/2008 4:26:05 PM
LastWriteTime  : 11/7/2008 4:26:15 PM
LastAccessTime : 11/7/2008 4:26:15 PM

Name           : Downloads
CreationTime   : 11/7/2008 4:26:05 PM
LastWriteTime  : 11/7/2008 4:26:15 PM
LastAccessTime : 11/7/2008 4:26:15 PM

The format-wide (fl) cmdlet allows a specific data field from the output to be displayed in a tabular form comprising a specific number of columns. For example, to display just the Name field of the get-childitems output as a table four columns in width, the following command would be executed:

PS C:\Users\Administrator> get-childitem | fw -column 4 name

Contacts                 Desktop                  Documents                Downloads
Favorites                Links                    Music                    Pictures
Saved Games              Searches                 Videos                   mydata.txt

The final formatting cmdlet is format-custom (fc) which displays the content of an object whilst visually representing the internal object hierarchy structure. This is a useful tool for learning about the way objects are structured within the PowerShell environment. For example, a process listing piped through the format-custom cmdlet displays output similar to the following:

PS C:\Users\Administrator> get-process -id 552 | fc

class Process
  Id = 552
  Handles = 114
  CPU = 0.71875
  Name = winlogon

As shown in the above output, the process object is an instance of the Process class which in turn contains Id, Handles, CPU and Name members.

Increasing levels of structure complexity may be viewed by using the -depth parameter. For an example of this in action, try executing the following command as an exercise:

 get-childitem | fc -depth 5