Using Windows PowerShell as an IT Pro – Part 7
In my last post I talked about how to pipe commands. Now I will talk about formatting the output of cmdlets.Windows PowerShell has a set of cmdlets that allow you to control which properties are displayed for particular objects and where you can send the output. The names of the cmdlets begin with the verbs Format and Out. I will begin by looking at some of the Format cmdlets.
The Format cmdlets let you select one or more properties to show. The Format cmdlets are: Format-Wide, Format-List, Format-Table, and Format-Custom.
Essentially they allow you to:
Make the output from commands easier to understand
See more detailed output
See only what you want to see
Each Format cmdlet has default properties that are used if specific properties are not selected for display. Each cmdlet also uses the same parameter name, Property, to specify which properties you want to display. Because Format-Wide only shows a single property, its Property parameter only takes a single value, but the property parameters of Format-List and Format-Table accept a list of property names.
When the Get-Service command is used, it provides a basic table of services containing three columns, including column headers. This table of services provides basic information about the services.
Get-Service m*

However, we can take the output of the Get-Service command and pipe it through the Format-List command.
Get-Service m* | Format-List

Here, the output looks quite a bit different and now displays a list of services and service properties. More properties of each service are displayed in the list view. Here, for example, information about related services, what state the service is allowed to be in, and the type of service it is are displayed.
The Format-Wide command shows only a single property of the services while displaying them in two columns. You can use the Format-Custom cmdlet to perform more advanced, customized output formatting with Windows PowerShell to help you get the output you need in the format you need.
Get-Service m* | Format-Wide

In my next post we will look at some more Format commands and see how to some more advanced formatting.
No comments:
Post a Comment