PowerShell: Formatting, Filtering & Comparing - Geek School

Working with Objects in the PowerShell Pipeline
This installment of Geek School focuses on the techniques of formatting, filtering, and comparing objects as they move through the PowerShell Pipeline.
Prior to delving into these methods, it's beneficial to review preceding articles within this series to establish a solid foundation.
Related Articles
Automating Windows with PowerShell: An introduction to scripting for system administration.
Cmdlet Usage in PowerShell: A guide to utilizing built-in commands.
Understanding Objects in PowerShell: A foundational explanation of object-oriented concepts within the PowerShell environment.
Further articles exploring these concepts will be published throughout the week, so be sure to check back for more in-depth coverage.
The PowerShell Pipeline allows for manipulation of data as it passes from one cmdlet to another. This includes altering the presentation and selecting specific objects based on defined criteria.
Effective object handling is crucial for creating efficient and targeted PowerShell scripts. Understanding how to filter and compare objects streamlines processes and improves script performance.
PowerShell's Default Output Formatting
Initially, PowerShell can seem complex, but its behavior is based on understandable principles. Understanding the formatting system is key to controlling output. For example, the Get-Service cmdlet, by default, presents only three properties: Status, Name, and DisplayName.
This limited display might seem odd, considering that the Get-Service cmdlet actually generates objects with many more properties. This can be verified by piping the output to Get-Member.

The discrepancy arises from a hidden configuration file that dictates how built-in cmdlets format their output. This file defines the default presentation of various object types.

Examining the Format File
To explore this configuration, open the following file using Notepad:
notepad C:\Windows\System32\WindowsPowerShell\v1.0\DotNetTypes.format.ps1xml
Within this file, utilize Notepad's Find function to locate the section pertaining to the ServiceController type. This will reveal how the Get-Service cmdlet's output is formatted.

The file demonstrates that PowerShell formats objects of the ServiceController type into a table displaying only the Status, Name, and DisplayName columns.
Handling Objects Without Defined Formatting
What happens when an object type lacks an entry in this format file, or any other formatting file? The behavior is straightforward.
If an object passed through the pipeline possesses five or more properties, PowerShell will display all of them as a list. Conversely, if the object has fewer than five properties, they will be presented in a tabular format.
- Objects with 5+ properties are displayed as a list.
- Objects with fewer than 5 properties are displayed as a table.
Customizing Data Output
When the standard display of objects or data types doesn't meet your needs, PowerShell allows for customized formatting. This is achieved through the use of three key cmdlets.
- Format-List
- Format-Table
- Format-Wide
Format-Wide presents a collection of objects by displaying a single property from each. It defaults to utilizing a 'name' property if available; otherwise, the first property, based on alphabetical order, is used.
Get-Service | Format-Wide
The cmdlet also defaults to a two-column layout, though this can be adjusted to specify both the desired property and the number of columns to display.
Get-Service | Format-Wide -Property DisplayName -Column 6
Should data be initially formatted as a table, it can be switched to a list view using the Format-List cmdlet. Consider the output generated by the Get-Process cmdlet as an example.
Get-Process | Format-List
While the tabular view is often suitable for this type of information, let's demonstrate how to view it in list format. This is accomplished by piping the output to Format-List.
By default, only the first four items are shown in the list. To reveal all object properties, a wildcard character can be employed.
Get-Process | Format-List –Property *
Alternatively, you have the option to select only specific properties for display.
Get-Process | Format-List –Property name,id
Format-Table, conversely, transforms data into a tabular format. Since the output from Get-Process is already presented as a table, we can leverage this cmdlet to selectively choose which properties are displayed within the table.
The AutoSize parameter was used to ensure all data fits within a single screen view.
Get-Process | Format-Table name,id –AutoSize
Filtering and Comparison in PowerShell
A significant advantage of employing an object-based pipeline lies in the ability to selectively remove objects at any point within the pipeline using the Where-Object cmdlet.
This allows for precise data manipulation and focused output.
Example of Filtering Services
Consider the following example:
Get-Service | Where-Object {$_.Status -eq "Running"}
This command retrieves all services and then filters the results to display only those currently in a 'Running' state.
Understanding the Where-Object Cmdlet
The Where-Object cmdlet functions through a straightforward process.
The variable $_ represents the current object being processed within the pipeline, enabling access to its properties for filtering purposes.
In the example above, we retain only those objects where the Status property is equivalent to 'Running'.
Comparison Operators
Several comparison operators are available for use within the filtering script block:
- eq (Equal To)
- neq (Not Equal To)
- gt (Greater Than)
- ge (Greater Than or Equal To)
- lt (Less Than)
- le (Less Than or Equal To)
- like (Wildcard String Match)
These operators facilitate a wide range of filtering criteria.
Further Information
A comprehensive list of operators and detailed information can be found within the about_comparison conceptual help file.
While mastering the Where-Object syntax may require some practice, the benefits of precise filtering are substantial.
