Using Windows PowerShell as an IT Pro – Part 8
In my last post I talked about the format cmdlets. Now I will continue to examine the format cmdlets.Using the Format-Table cmdlet with no property names specified to format the output of the Get-Process command, provides exactly the same output returned without performing any formatting. The reason is that processes are usually displayed in a tabular format, as are most Windows PowerShell objects.
The displayed properties can be controlled by any of the format cmdlets by leveraging the property parameter. In this example, the format of the table displays with three specific properties.
Get-Service m* | Format-Table -Property Status, Name, ServiceType

Before I show an example of a more advanced formatting command, let’s look at some sorting. The Sort-Object cmdlet sorts objects in ascending or descending order based on the values of properties of the object.
You can specify a single property or multiple properties (for a multi-key sort) and you can select a case-sensitive or case-insensitive sort. You can also direct Sort-Object to display only the objects with a unique value for a particular property. This command displays a sort of services by status.
Get-Service m* | Sort-Object Status

You can also perform advanced formatting by combining formatting commands and adding some grouping. This command sorts the service list by status, before passing the output to the format-table command, which groups the output by status. In the displayed output the stopped and running services are grouped together, and under each status is a table containing the name and display name of each service.
Get-Service m* | Sort-Object Status | Format-Table –Group Status -Property Name, DisplayName

In my next post we will look at the Output commands.
No comments:
Post a Comment