Changes

Jump to: navigation, search

Directing and Formatting Windows PowerShell 1.0 Output

3,295 bytes added, 20:43, 14 November 2008
New page: It will be extremely rare that a Windows PowerShell interactive shell session or script execution that does not need to display output of one form or another. Fortunately, in recognition o...
It will be extremely rare that a Windows PowerShell interactive shell session or script execution that 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 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":

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

== 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:

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

== 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 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 ==

Navigation menu