Chapter 26. Custom formatting views

 

This chapter covers

  • Changing object type names
  • Creating view types
  • Loading view types
  • Using named views

One of the biggest features of PowerShell’s formatting system is its ability to use predefined layouts, or views, to display specific types of objects. When you run a cmdlet like Get-Process or Get-Service, the display you see by default is controlled by a set of predefined views that Microsoft provides along with PowerShell. The default display is a best guess at what you’re most likely to want to see. Those guesses don’t always work out:

PS C:\> Get-WmiObject -Class Win32_OperatingSystem

SystemDirectory : C:\windows\system32
Organization    :
BuildNumber     : 9600
RegisteredUser  : richard_siddaway@hotmail.com
SerialNumber    : 00258-70157-84935-AAOEM
Version         : 6.3.9600

What you need to do is select the data you want:

PS C:\> Get-WmiObject -Class Win32_OperatingSystem |
 select PSComputerName, Caption, ServicePackMajorVersion,
 SystemDirectory, OSArchitecture

PSComputerName          : RSSURFACEPRO2
Caption                 : Microsoft Windows 8.1 Pro
ServicePackMajorVersion : 0
SystemDirectory         : C:\windows\system32
OSArchitecture          : 64-bit

You can also create your own predefined views, either to override the ones Microsoft provides or to provide default formatting for custom objects that you create.

26.1. Object type names

26.2. Getting view templates

26.3. Starting a view file

26.4. Adding view types

26.5. Importing view data

26.6. Using named views

26.7. Going further

26.8. Summary