Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get PowerShell View Definitions

Posted on June 7, 2011September 3, 2018

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d