Understanding Format cmdlets view parameter

Sometimes the quickest way to discover something new in PowerShell is by making an typo error.

I wanted to know how many different views existed for the Get-Process cmdlet.

Although there are probably many ways to find the answer, the quickest one is by trying to guess it.

Get-Process | Format-Table -View GuessOrTypo


As you can see in the above screenshot, I got my answer straight away in the error message.
Yes, you should read carefully errors messages, they are most of the time useful and can even contain the answer on how to fix your typo.

If you’ve a problem with the color of error messages. You can change it to a different color

#requires -version 3.0
 if ($ise) {
    $Host.PrivateData.ErrorForegroundColor = [System.Windows.Media.Color]'Green'
 } else {
    $Host.PrivateData.ErrorForegroundColor = [system.ConsoleColor]::Green
 }

Let’s see what these views look like:

Get-Process -Id $PID
Get-Process -Id $PID | Format-Table -View process
Get-Process -Id $PID | Format-Table -View Priority
Get-Process -Id $PID | Format-Table -View StartTime

Another way of listing views that apply to the System.Diagnostics.Process object consists in digging into its formatting XML definition file

(
  [xml](Get-Content "$PSHOME\DotNetTypes.format.ps1xml")
).SelectNodes(
'//ViewSelectedBy[TypeName = "System.Diagnostics.Process"]/..'
)

The 4th view that you can see in the above snapshot that isn’t a Table is actually associated to the Format-Wide cmdlet

Get-Process | Format-Wide -View process

If you want to understand how PowerShell formats objects to display them, you can read more on this subject on these pages:

…and last but not least in the built-in help of PowerShell thanks to this shortcut:

Get-Help about_format.ps1xml
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.