PowerShell Format Fundamentals
No more wading through xml files to get what you need. The Get-FormatData cmdlet offers some nice data presentation.
- By Jeffery Hicks
PowerShell is all about the objects. At the end of your pipelined expression PowerShell checks the objects. The type of object and the number of properties guide PowerShell in how to format the results. PowerShell depends on a set of configuration files found in the $PSHOME folder. These are special XML files with a .ps1xml file:
PS C:\> dir $pshome\*.ps1xml
You can view these files in Notepad or any XML editor. But a word of warning, do not modify these files as they are digitally signed by Microsoft. These files are organized by object type and dictate whether you see a table or a list. Some object types even have special "views" that display a preselected set of properties. For example, try this:
PS C:\> get-process | format-table -View priority
You still get process objects but organized with a different set of properties. The challenge has always been finding these special views. In v1 you had to manually wade through the xml file. Now, we can use a cmdlet called Get-FormatData.
Here's a simple way to first see all of the object types currently configured:
PS C:\> get-formatdata | sort TypeName | Select Typename
These are the object type names that you see when you pipe something to Get-Member. The information for a given object type is buried a bit so use an expression like this to discover what is available:
PS C:\> get-formatdata -TypeName system.diagnostics.process | select -expandproperty FormatViewDefinition
These are the available views for a process object. The first is the default. You can use the other 3 by invoking the -View parameter of the Format cmdlet that corresponds to the Control property. Try these:
PS C:\> get-process | format-table -View StartTime
PS C:\> get-process | format-wide
As you look through all of this you'll also discover other ways to view information. Take a look at this:
PS S:\> Get-FormatData fileSystemTypes | Select -ExpandProperty FormatViewDefinition
What this implies is that when you run Get-ChildItem and pipe the results to one of the format cmdlets you'll get a different view. Try it out and see for yourself.
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.