Changes

Jump to: navigation, search
no edit summary
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 ==
== 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":
<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>
== 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.
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 my 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'':
<pre>
get-childitem | fc -depth 5
</pre>
 
<google>BUY_WPS_BOTTOM</google>

Navigation menu