MCPMag.com

Sign up for our newsletter.

I agree to this site's Privacy Policy.

Prof. Powershell

Get Your PowerShell Object Properties In Order

PowerShell 3 deploys a new type adapter that will have you seeing hash table output in the order you want them to appear.

In PowerShell 2, a common technique for creating a custom object is to create a hash table of property values and then use the hash table with New-Object:

PS C:\> $hash=@{
>> Name="Jeff"
>> Title="Prof. PowerShell"
>> Version=$host.version
>> OS=$(Get-wmiobject win32_operatingsystem).caption
>> }
>>
PS C:\> new-object psobject -Property $hash

Name   Version   Title              OS
----   -------   -----              --
Jeff   2.0       Prof. PowerShell   Microsoft Windows 8 Pro

This works, but notice the output. The property values are not displayed in the order I entered them in the hash table. The problem is that in version 2 hash tables are unordered. There is no predicting what order the values will be displayed. The long way around this problem in version 2 is to create a custom format extension. But that's a lot of work.

In PowerShell 3, we finally have ordered hash tables. You create the hash table the same way, but this time use the [ordered] type adapter:

PS C:\> $hash=[ordered]@{
>> Name="Jeff"
>> Title="Prof. PowerShell"
>> Version=$host.version
>> OS=$(Get-wmiobject win32_operatingsystem).caption
>> }
>>
PS C:\> new-object psobject -prop $hash

Name   Title            Version  OS
----   -----            -------  --
Jeff   Prof. PowerShell  3.0     Microsoft Windows 8 Pro

You must put [ordered] right before the hash table, but notice the result. Now the property names are in the same order.

And that's not all. PowerShell 3 also has another type adapter called [pscustomobject]. Now you can create a custom object without having to resort to the New-Object cmdlet. Use the adapter in front of any hash table to turn it into an object:

PS C:\> [pscustomobject]@{
>> Computername = $(Get-wmiobject win32_operatingsystem).csname
>> OS = $(Get-wmiobject win32_operatingsystem).caption
>> Uptime = (get-date) - ([wmiclass]"").ConvertToDateTime((Get-wmiobject win32_operatingsystem).LastBootUpTime)
>> Make = $(get-wmiobject win32_computersystem).model
>> Manufacturer = $(get-wmiobject win32_computersystem).manufacturer
>> MemoryGB = $(Get-WmiObject win32_computersystem).TotalPhysicalMemory/1GB -as [int]
>> Processes = (Get-Process).Count
>> }
>>

Computername : SERENITY
OS           : Microsoft Windows 8 Pro
Uptime       : 13.08:54:46.1946242
Make         : Qosmio X505
Manufacturer : TOSHIBA
Processes    : 115
MemoryGB     : 8

How easy is that?! If for some reason the hash table isn't ordered, you can combine the type adapters:

[pscustomobject][ordered]@{...

These are great tools that make it even easier to create custom objects from the PowerShell console. In a script, you might still want to use New-Object simply because it is clearer than [pscustomobject]. But I would still use [ordered] when defining the hash table.

So get to PowerShell 3 and get your object properties in order.

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org. Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)

comments powered by Disqus

Reader Comments:

Mon, Mar 18, 2013 OldguardMD

How do you add rows?

Thu, Dec 13, 2012 mjolinor

Also note that an ordered hash table has both keys and indexes. Elements can be referenced by either their index number or their key. They can even have the same index and key for different elements, but the selected element will be determined by whether the reference is an [int] or [string]. $hash[0] will be the element with array index 0. $hash['0'] will be the element with key name '0'. They may or may not be the same element.

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above