Categories

Property Order

A comment was left on my recent post regarding the creation of objects that implied using a hash table (in the –property parameter of New-Object) could cause problems because there is no control over the order of the properties.

My question is why do you need to control the order of the properties. The whole point of PowerShell is that we are working with .NET based objects whose properties have names. We work with the names not property number 1 or property number 3.

This function creates a new object based on the returns of get-process

001
002
003
004
005
006
007
008
009
010
011
012

function test-process {
get-process | foreach {
 $process = New-Object PSObject -Property @{
   Name = $_.ProcessName
   Id = $_.Id 
   Handles = $_.Handles
   StartTime = $_.StartTime
   Responding = $_.Responding
 }
 $process
}
}

 

Run the script to create the function then try these commands

test-process                                                                                                                                                      

test-process | ft -a                                                                                                                                              
test-process | sort name | ft –a                                                                                                                                  

test-process | Export-Csv tp.csv                                                                                                                                  

Import-Csv tp.csv                                                                                                                                                 
Import-Csv tp.csv | ft –a  

Import-Csv tp.csv | select Name, Starttime | sort starttime -desc | ft

 

Everything works -  we have objects with properties and we work with the properties by name

We shouldn’t need to care about the order of the properties on an object.  I can see a vague argument that if we are trying to use the valuefrompipeline attribute on an advanced function we may have problems but we can easily overcome that by adding ValueFromPipelineByPropertyName.

I have never seen a problem caused by the order of properties.  If you have seen an issue please leave an example.

3 Responses to Property Order

  • Thanks for this, Richard.

    I ran into this a couple of weeks ago. I finally realized that if I needed a certain order to “select-object” them that way, or set the order in the format-table cmdlet.

    Enjoy your posts.

  • Jesse says:

    “Need” is perhaps too strong a word, but I often desire a specific order of properties for output. When I use cmdlets like Export-CSV, Out-GridView, it really bugs me when columns are seemingly random. If you’ve got a column you consider something like the primary key, you generally want it on the left instead of in the middle of the rest of the data.

    So I do use Select-Object quite a bit. Right now I’ve got a data set with about 35 columns, and only wish to set the order of the first 5, leaving the rest after unsorted. Not sure how I’m going to tackle this.

  • RichardSiddaway says:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>