34,333
edits
Changes
no edit summary
== 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 its 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 ''get-childitem'' which displays the contents of a directory. For example:
<pre>
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
</pre>
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 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>
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
</pre>
The ''format-wide'' (fl) cmdlet allows a specific data field from the output to be displayed in tabular form comprising a specific number of columns. For example, to display just the ''Name'' field of the ''get-childitems'' output is a table 4 columns is width, the folling command would be executed:
<pre>
PS C:\Users\Administrator> get-childitem | fw -column 4 name
Contacts Desktop Documents Downloads
Favorites Links Music Pictures
Saved Games Searches Videos mydata.txt
mydatafile.txt
</pre>
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:
<pre>
PS C:\Users\Administrator> get-process -id 552 | fc
class Process
{
Id = 552
Handles = 114
CPU = 0.71875
Name = winlogon
}
</pre>
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:
<pre>
get-childitem | fc -depth 5
</pre>