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:
- 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.
- 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.
- 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:
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
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“.