Creating Custom Objects in PowerShell: v3.0 Enhancements

Posted on August 31, 2012 by Jeff Hicks in PowerShell with 0 Comments

We’re at the end of our multi-part series on creating custom objects in PowerShell! In part one, I discussed the basics of creating custom objects in PowerShell to meet your needs; however, in this version, we lost the original object. In part two, I showed you how to modify and create the custom object while retaining the original. In part three, we learned how to create a custom object from scratch.

Everything I’ve demonstrated up to now is intended for PowerShell 2.0. My examples should also work in the upcoming PowerShell 3.0, but v3 offers some terrific enhancements. Today, I’ll go deeper and include the enhancements found in PowerShell 3.0.

First, instead of using New-Object and passing a hash table of property values, we can easily create an object with the [pscustomobject] type accelerator.

 

I’m still creating a hash table, but in front I’m using the type accelerator. The end result is a custom object.

 

But it gets better. If you recall from a previous article, there was a subtle issue when using hash tables for property values: you couldn’t control the order. That is still true in PowerShell 3.0. Here’s a simple hash table.

 

When I write it to the pipeline as a custom object I get this:

Sponsored

Not easy to read and not what I want. But now, we can use another new attribute called [ordered] when defining the hash table.

 

Now, the table order is maintained both as a hash table and when turned into an object.

 

I love this. Now it is even easier to create rich objects that can live in the pipeline without complicated Select-Object statements or formatting extension files.

Sponsored

 

The end result is a collection of objects that I can use with other PowerShell commands.

 

It’s important to remember, especially with regards to PowerShell 3.0, is to make sure you know how to use hash tables. I suspect they will play an even bigger role in your PowerShell work.

Once again, as we’ve seen here and throughout this series, PowerShell is all about the objects. There are a variety of ways to create and modify them. You’ll also most likely see examples of all these techniques in scripts you find on the Internet. But now, I trust you are eager to put your new found knowledge to work. If you run into issues or have questions, feel free to post in the Ask Don and Jeff forum at PowerShell.com.

Sponsored

Tagged with , , ,