#Requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Get-View.ps1 # Version: 1.0 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # http://www.ScriptingGeek.com # Date: 6/7/2011 # Keywords: # Comments: # # "Those who forget to script are doomed to repeat their work." # # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** # ----------------------------------------------------------------------------- Function Get-View { <# .SYNOPSIS Get defined views from DotNetTypes.format.ps1xml .DESCRIPTION This function will parse the DotNetType.Format.ps1xml file to discover all named views. The default behavior is to return views that are most likely alternate views. Once you identify the control and object class, you can run the appropriate cmdlet and pipe the expression to the corresponding format cmdlet. For example, the Process object has a Table view called priority. PS C:\> get-view table | where {$_.class -eq "System.Diagnostics.Process"} Class Format View ----- ------ ---- System.Diagnostics.Process Table process System.Diagnostics.Process Table Priority System.Diagnostics.Process Table StartTime Once this is known, you can run an expression like this: PS C:\> Get-Process | Format-Table -View Priorty .PARAMETER Control The type of control to search for: Table, List, Wide or Custom. The default is to return all controls. .PARAMETER All Return all views. The default is to return views that are most likely alternate views. .EXAMPLE PS C:\> Get-View -control list Class Format View ----- ------ ---- System.DirectoryServices.Directory... List DirectoryEntry System.Management.Automation.PSSna... List PSSnapInInfo Get all list control views. .NOTES NAME : Get-View VERSION : 1.0 LAST UPDATED: 6/7/2011 AUTHOR : Jeffery Hicks .LINK http://jdhitsolutions.com/blog/2011/06/get-powershell-view-definitions/ .INPUTS None .OUTPUTS Custom object #> [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