Giving Type Names to Your Custom Objects

Hello again!

This week I wanted to write about something that I found to be really useful but that I keep forgetting.  Ideally by committing it to a blog I’ll be able to remember it going forward! 

So here’s the deal, when you make a PSCustom object, using New-Object, Select-Object, or the more modern, [PSCustomObject]@{}, you can capture and modify it before releasing it back into the pipeline.


So that leaves me with two questions, how and why?

I’ll start with Why.

It’s worthwhile to add your own custom types for a bunch of reasons. The two top reasons are custom formatting and object filtering. 

Once you define a type you can define how it is displayed and handled by the host.  It’s really nice when you want to make a detailed return object for your query but users only want to see a couple key properties.  I’m often guilty of dropping select statements at the end of my functions just to clean up my output object.  That is really not a good way to handle this situation.  Custom types are the clean, natively supported by the shell, and they leave you with rich objects to work with when you need them.

Object filtering is the other big advantage. When you make your functions or scripts it’s handy to define what types of objects you accept. PSObject is obviously a pretty broad type, defining custom types for your objects allows you to limit your inputs to those custom types.  Which is really handy when you’re writing custom modules for environment specific tasks. I’ve written a module that works with log entries and data for a call center management application called Genesys. Until I buckled down and added custom types I was often getting conflicts when piping these functions together. 

Now we get onto the how, luckily for all of us that’s pretty easy.

$entry = [PSCustomObject]@{

  Property1 = $value1

  Property2 = $value2



The new typename in this case is JasonsObject.CustomType1.  The name is arbitrary but I like to structure the objects in kind of a standard object format.  Majortype.Minortype.SpecificClass.  You don’t need to follow my lead on this, use whatever works well in your environment.


And that’s all I have for this week.  Next up I’ll try and do an article on making the supporting formatting document.


This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s