When you write objects to the pipeline in Windows PowerShell, at the end of the pipeline PowerShell's formatting system handles displaying the results to the console. It accomplishes this by using a set of rules stored in XML configuration files. This is why when you run Get-Process you get a table with a pre-defined set of properties. But sometimes there are alternate views defined. For example, for process objects there is a table view called Priority. Once you know the view name you can use it.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Get-Process | Format-Table -view Priority
The difficult part is finding what alternate views have been defined. For that, you can use my Get-View function.
This function parses out the XML configuration file, searching for view definitions.
Function Get-View { [cmdletBinding()] Param( [Parameter(Position=0)] [ValidateSet("Table","List","Wide","Custom")] [string]$Control, [switch]$All ) #get dotNetTypes xml data Write-Verbose "Parsing $pshome\dotNettypes.format.ps1xml" [xml]$xml=Get-Content -Path $pshome\dotNettypes.format.ps1xml if ($Control) { Write-Verbose "Getting $control views only" } #initialize an array to hold view objects $views=@() #get the view definitions $xml.configuration.viewdefinitions.view | foreach { #find what type of control to use Switch -regex ($_.Get_InnerXML()) { "TableControl" {$format="Table"} "ListControl" {$format="List"} "WideControl" {$format="Wide"} "CustomControl" {$format="Custom"} default {$format="Unknown"} } #add an object to the array $views+=New-Object -TypeName PSObject -Property @{ View= $_.name Class=$_.ViewSelectedBy.TypeName Format=$format } } #foreach Write-Verbose "Found a total of $($views.count) views before filtering" #by default only get custom views which unless -All is specified if (-Not $All) { $views=$views | Where {$_.Class -ne $_.View} } #if a specific control type was called for, then only write #matching objects if ($Control) { $views | where {$_.Format -eq $Control} } Else { #else write all objects $views } Write-Verbose "Finished" } #end Function
The function uses a Switch contruct to parse out the inner xml for each view to identify whether it is a table, list, wide or custom.
$xml.configuration.viewdefinitions.view | foreach { #find what type of control to use Switch -regex ($_.Get_InnerXML()) { "TableControl" {$format="Table"} "ListControl" {$format="List"} "WideControl" {$format="Wide"} "CustomControl" {$format="Custom"} default {$format="Unknown"} }
I then take data from each XML node and add a custom object to a temporary array.
$views+=New-Object -TypeName PSObject -Property @{ View= $_.name Class=$_.ViewSelectedBy.TypeName Format=$format }
The temporary array is so that I can write filtered data to the pipeline in a few ways. One thing you have to keep in mind is that default views are also defined here. Typically, but not always, if the view name matches the class name, it is a default view. So by default I filter those out, unless you use the -All parameter.
if (-Not $All) { $views=$views | Where {$_.Class -ne $_.View} }
The second type of filtering you can do is by the control. The default behavior is to show you everything. But you can limit your search to a particular control like List.
if ($Control) { $views | where {$_.Format -eq $Control} } Else { #else write all objects $views }
Once loaded into your PowerShell session, you might use it like this:
PS E:\> get-view table | format-table -auto CClass Format View ----- ------ ---- System.Collections.DictionaryEntry Table Dictionary System.Diagnostics.ProcessModule Table ProcessModule System.Diagnostics.Process Table process System.Management.Automation.PSSnapInInfo Table PSSnapInInfo System.Diagnostics.Process Table Priority System.Diagnostics.Process Table StartTime System.ServiceProcess.ServiceController Table service
Once I've discoverd that the PSSnapin object has an alternate table view, I can use it.
The complete script includes comment based help. Download Get-View.