Group by Different Properties for Format-Table

Avatar

Heath

For my Windows Installer PowerShell Extensions, I’ve been simplifying some of the use cases and adding additional formats. One thing I wanted to do was display source list information in a table and group either by the attached ProductCode or PatchCode properties. The format-table cmdlet doesn’t support multiple properties and doesn’t appear to allow you to condition the label. It also seemed that Display.xml, or *.formats.ps1xml files, didn’t natively support grouping by multiple property names.

The help topic about_Display.xml hinted at a CustomControl tag, but examples of this and related tags in the default *.formats.ps1xml files offered little insight about their true power. With some help from a simple user-generated XML schema and some trial and error, I was able to display a label with an appropriate value conditionally, such that the following expressions displayed the partial table shown below.

I accomplished this by using a scriptblock to group, but referencing a CustomControl with multiple ExpressionBinding elements as shown in the partial example below.

Hopefully this serves as a good example of more powerful features of formatting in PowerShell.

Avatar
Heath Stewart

Senior Software Engineer

Follow Heath   

0 comments

    Leave a comment