PSCustomObject NoteProperties Appear in Random order

by Jim at 2012-10-01 10:19:03

When I write a function that produces a PSCustomObject using syntax like the following:

New-Object -Type PSOBject -Prop $properties

the order in which the properties are displayed seems random. I know I can force the sequence by using Select-object, but that seems inefficient.

Am I missing something?

Thanks,
Jim
by Jim at 2012-10-01 11:25:54
I finally found an excellent blog entry by Shay Levy that addresses my issue!

http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2012/04/13/Custom-objects-default-display-in-PowerShell-3-0.aspx

Thanks anyway and hopefully this helps others in the forum!
by RichardSiddaway at 2012-10-02 05:13:10
In PowerShell v3 create an ordered hash table for the properties to preserve the order. The other question is why do you need to force the order of properties?
by Jim at 2012-10-04 17:54:40
I created an object containing numerical data such that I wanted to see its properties in the console in a particular sequence. (The data is Win 7 boot performance data read from xml). I did not want to be forced to pipe it to select-object every time I ran the function at the prompt. I use the function very often.
In other words, ME = LAZY, or laziness is the mother of invention (or advanced functions)!
:slight_smile:

Thanks to Shay’s blog post, I did exactly what you also suggested. Version 3 rocks!

I really appreciate the feedback!

-Jim
by Lembasts at 2012-10-07 14:10:46
This was also helpful for me as I create a csv file from a new object and its easier to analyse the data when its in a certain order.

Thanks