PowerShell QuickTip: Using PSTypeNames to See the TypeName and Inheritance Chain

I’ve mentioned plenty of times that PowerShell is all about .NET objects, well actually PowerShell "adapted" .NET objects.  Quite often it is handy to know which type of object you are dealing with.  You have a couple of options here:

PS> (get-date).GetType().Fullname

That uses the GetType method that is available on all .NET objects.  However PowerShell also allows you to use COM objects which don’t (typically) have the GetType method available.  However all objects you use in PowerShell have been adapted by what is called the "Extended Type System".  That is, PowerShell dynamically wraps .NET and COM objects to provide standardized features.  One of those is a property called PSTypeNames that is available on *all* objects you will encounter in PowerShell – both .NET and COM.  You can use it like so to get the object’s typename:

PS> (get-date).pstypenames[0]
System.DateTime

To get an object’s complete inheritance chain, just get the pstypenames property like so:

PS> (get-date).pstypenames
System.DateTime
System.ValueType
System.Object

Advertisements
This entry was posted in PowerShell. Bookmark the permalink.

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 )

Google+ photo

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

Connecting to %s