Lesson 2: Selecting, sorting, and measuring objects
In this lesson, you will learn to manipulate objects in the pipeline by using commands that sort, select, and measure objects. Selecting, sorting, and measuring objects is essential to successfully creating automation in Windows PowerShell.
Lesson objectives
After completing this lesson, you will be able to:
• | Explain how to sort objects by a specified property. |
• | Sort objects by using the Sort-Object command. |
• | Explain how to measure objects’ numeric properties. |
• | Measure objects by using the Measure-Object command. |
• | Explain how to display a subset of objects in a collection. |
• | Explain how to display a customized list of objects’ properties. |
• | Select objects by using the Select-Object command. |
• | Explain how to create calculated properties. |
• | Create custom calculated properties for display. |
Sorting objects by a property
Some Windows PowerShell commands produce their output in a specific order. For example, the Get-Process and Get-Service commands produce output that is sorted alphabetically by name. Get-EventLog produces output that is sorted by time. In other cases, the output may not appear to be sorted at all. Sometimes, you may want command output sorted differently from the default. The Sort-Object command, which has the alias sort, can do that for you.
Sort-Object
The Sort-Object command accepts one or more property names to sort by. By default, the command sorts in ascending order. If you want to reverse the sort order, add the -Descending parameter. If you specify more than one property, the command first sorts by the first property, then by the second property, and so on. It is not possible in a single command to sort by one property in ascending order and another in descending order.
The following commands are all examples of sorting:
By default, string properties are sorted without regard to case. That is, lowercase and uppercase letters are treated the same. The parameters of Sort-Object allow you to specify a case-sensitive sort, a specific culture’s sorting rules, and other options. As with other commands, you can view the help for Sort-Object for details and examples.
Grouping objects by property
Sorting objects also allows you to display objects in groups. The Format-List, Format-Table, and Format-Wide formatting cmdlets all have a -GroupBy parameter that accepts a property name. By using the -GroupBy parameter, you can group the output by the specified property. For example, the following command displays the names of services running on the local computer in two two-column lists that are grouped by the Status property:
The -GroupBy parameter works similarly to the Group-Object command. The Group-Object command accepts piped input and gives you more control over the grouping of the objects. Group-Object has the alias group.