PSObject sorting

Question:

I am trying to format a PSObject related to a question a few days back. My object looks like this:

The above gives the following output:

However, this:

gives the following output:

Not really a big deal, but I wonder why the formatting changes? It does not seem to be alphabetical in any way. Furthermore, I tried sorting the object with Sort-Object (from A-Z) but to no avail. Is it String related?

Answer:

The order of a hashtable can’t be predicted (in PowerShell V3.0 you can user the [ordered] accelerator to make an hashtable ordered), but in V2.0 you need to build your custom object like this to preserve properties order:

Source:

PSObject sorting by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply