I’ve started a new job (thus the relative quiet lately), and one of the first tasks was to perform information gathering via PowerShell. Luckily, this is old hat. Doubly luckily, I have some coworkers that know a ton about PowerShell! Today I’m going to talk about custom objects.
In the snippet above, we create a new hash table with the keys FirstName, LastName, BirthDay, and Age. In the same step we assign values each key. We then call the New-Object cmdlet and specify the properties of this new object to be the freshly created hash table.
An alternate method for creating a custom object is leveraging the Add-Member cmdlet. The code below creates an identical $person object:
Both of these are great, but there are some challenges. Using the $prop option results in unreliable ordering (e.g. the output object’s properties may be in alphabetical order, or FirstName, BirthDay, LastName, Age - or completely different). The Add-Member method will keep the order, but is very wordy (476 characters vs 326 without tabbing/white space).
That’s when I learned of ordered hashes:
This will ensure that the resulting object will always have the properties in the order specified (FirstName, LastName, BirthDay, Age).
But that’s not all! There has been a way to create a Custom Object using only the hash (and the properties remain ordered) since the PowerShell v3 release candidate.
We’ve dropped the wordiness down to 278 characters while maintaining readability!