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:
- How the PowerShell Formatting Subsystem Works by Don Jones
- How PowerShell Formatting and Outputting REALLY works by Jeffrey Snover
- PSStandardMembers – The Stealth Property by Jeffrey Snover
- Recipe called “Define Custom Formatting for a Type” in the online book of Lee Holmes
…and last but not least in the built-in help of PowerShell thanks to this shortcut:
Get-Help about_format.ps1xml