Home > PowerGUI, PowerShell > Essential PowerShell: Name your custom object types

Essential PowerShell: Name your custom object types

PowerShell is a very flexible scripting language that allows users to dynamically create and/or extend objects with additional methods and properties.  This is very useful when you’re trying to build up a rich data set with all of the properties or methods you need.  One important thing that is often overlooked when people are writing scripts that do this is that they can also give those objects a type name.  Why is this type name important?  Three reasons:

  1. If you want to further extend those types automatically via a type data file, you’ll want a unique type name so that only the appropriate objects are extended.
  2. If you want to apply specific default custom formatting to those types via a format data file, you’ll want a unique name so that only the appropriate objects are formatted this way.
  3. If you want to associate specific links and actions with your custom object type in PowerGUI, you’ll want a unique name so that you don’t get links and actions associated with other types.

In practice there are only two use cases where I need to create a custom object type name, and I apply different names depending on the scenario I’m working with at the time.

If I have created a brand new generic PSObject, then I apply a name appropriate to the object.  In this case, after I created my custom object and added the properties, I would do the following:

$myObject.PSObject.TypeNames.Insert(0,’MyObjectTypeName’)

Alternatively, if I am extending an object of a particular type, then I apply an extended type name for that object to the modified version.  In this case, after I created my custom object and added the properties, I would do the following:

$derivedTypeName = $myObject.PSObject.TypeNames[0]
$myObject.PSObject.TypeNames.Insert(0,”$derivedTypeName#MyExtensionName”)

Then if you create type or format data files, you simply need to use your new type name in appropriate XML attribute to set up the association.  Or if you’re adding functionality to PowerGUI, any links and actions you create will automatically be associated with the lowest derived object type, which will be the type name you applied to the object before outputting it in the PowerGUI data grid.

You can add as many type names as you want to your objects, so if you want to create a virtual object hierarchy with your custom object types, and then associate format or type data specifications with derived or base object types, you can do that as well through multiple calls to the Insert method on the TypeNames collection.  This can be useful if you want to share a certain set of functionality between two types of objects you are creating but in addition you want some functionality to be specific to each type and you don’t want to duplicate code.

Hopefully this will encourage you to name your custom object types and define their default properties in scripts that you share with others.

If you’re working with custom objects you should also check out the follow-up post to this that resulted from Hal’s comments on this post, titled “Essential PowerShell: Define default properties for custom objects“.

Enjoy!

Kirk out.


Share this post:

  1. July 3, 2008 at 5:34 pm | #1

    This is a really cool technique, Kirk, thanks. The only thing I regret is that type formatting must be done via external files. I suppose you could generate a ps1xml inline and call it but that ought not be necessary.

  2. Kirk Munro
    July 3, 2008 at 9:42 pm | #2

    You know what, I started typing in a reply saying that someone should create one or more scripts, functions, cmdlets, etc. to handle this sort of thing, and then it dawned on me that in many cases, if not the majority, you don’t even really need it. I mean, realistically, when you create a custom object all you really care about are the default properties to display in your output, right? You’re already adding your own members on the fly, so you don’t really need a types.ps1xml file, and any other formatting you would want to apply can be done using expressions in the Format-Table or Format-List cmdlet. Thanks for making me think twice about this and check out this follow-up post: http://poshoholic.com/2008/07/05/essential-powershell-define-default-properties-for-custom-objects/!

  3. July 9, 2008 at 11:26 pm | #3

    great to see somebody else caring about this and using this technique. I thought i was the only one.

  4. Mike Shepard
    December 20, 2008 at 7:53 pm | #4

    Great tip! I have a lot of functions that return lists of datarows. By adding a typename that is more descriptive, I can create formats and type definitions for them in ps1xml files. Makes things look a lot nicer.

  5. Aten
    October 6, 2012 at 1:36 pm | #5

    Thanks for the help. It Helped to accomplish my goal but i had to correct a few things

    • Kirk Munro
      October 6, 2012 at 3:29 pm | #6

      Glad it helped. What did you have to correct?

  1. July 5, 2008 at 2:25 am | #1
  2. July 7, 2008 at 7:26 am | #2
  3. June 9, 2009 at 5:02 am | #3
  4. July 5, 2011 at 4:57 am | #4
  5. October 11, 2013 at 4:12 am | #5

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

Follow

Get every new post delivered to your Inbox.

Join 47 other followers

%d bloggers like this: