Secret -Force Switch

by May 19, 2009

PowerShell automatically displays all object properties when you output the object to the console. Here is an example:

Get-WmiObject win32_bios

Actually, PowerShell uses its internal type system to select which object properties to show. This is smart because most objects have plenty of properties, and if you tried to show them all, your console would have a hard time displaying this wealth of information in a readable manner.

To really view all object properties, you should pass the object to Format-List and add an asterisk:

Get-WMIObject Win32_BIOS | Format-List *

This will not always work in rare circumstances. Here is an example where the $error automatic variable stores all errors that occurred in your session. This is actually an array, and the first element returns the last (most current) error record:

$error[0]

If you don't get back anything, no error has occurred yet. In this case, you should simply cause an error like so:

1/$null

Now, the strange thing happens when you try and output the error record Exception property:

$error[0].Exception

For some reason, you only get back the error text message. Things do not change when you pass on the result to Format-List:

$error[0].Exception | Format-List *

In these situations, you can use the -Force switch to bypass the type system. Now, this all works as expected:

$error[0].Exception | Format-List * -force