none
Extract PowerShell Object Properties Default Ordering RRS feed

  • Question

  • Hi Eveyone,

    I seem to be stuck trying to figure out something. I have two object with exact same data and values in order, the difference is that the column\header\PSproperties ordering is not same. I know using Select-Object we can have our custom formatting, but I don't want that.

    What I have done is Import-Csv a csv file with headers as  Name,Status,DisplayName and another with Status,DisplayName,Name .

    Now when I view the data using $pdata | Format-List or Format-Table the ordering is as expected.

    But when I check it using | Get-Member, the order is DisplayName,Name,Status -Alphabatically default hashtable ordering I guess.

    I have a requirement of using | Get-Member to extract the header\properties information from the PSObject, but in the correct \default order. So can't use Select-Object here.

    PS Script> ($pdata | gm -MemberType NoteProperty).Name
    DisplayName
    Name
    Status


    I got to know that PSObject uses ordered hastable for properties ordering.
    But need to know how it identifies remembers the order and how can we extract it in the same order.

    Here is a snippet of my testing.

    PS Script> $pdataS.Item(0) | fl
    
    
    Status      : Stopped
    DisplayName : Peer Networking Identity Manager
    Name        : p2pimsvc
    
    
    
    PS Script> $pdata.Item(0) | fl
    
    
    Name        : p2pimsvc
    Status      : Stopped
    DisplayName : Peer Networking Identity Manager
    
    
    PS Script> $pdataS | gm
    
    
       TypeName: Selected.System.Management.Automation.PSCustomObject
    
    Name        MemberType   Definition
    ----        ----------   ----------
    Equals      Method       bool Equals(System.Object obj)
    GetHashCode Method       int GetHashCode()
    GetType     Method       type GetType()
    ToString    Method       string ToString()
    DisplayName NoteProperty System.String DisplayName=Peer Networking
    Name        NoteProperty System.String Name=p2pimsvc
    Status      NoteProperty System.String Status=Stopped
    
    
    PS Script> $pdata | gm
    
    
       TypeName: Selected.System.Management.Automation.PSCustomObject
    
    Name        MemberType   Definition
    ----        ----------   ----------
    Equals      Method       bool Equals(System.Object obj)
    GetHashCode Method       int GetHashCode()
    GetType     Method       type GetType()
    ToString    Method       string ToString()
    DisplayName NoteProperty System.String DisplayName=Peer Networking
    Name        NoteProperty System.String Name=p2pimsvc
    Status      NoteProperty System.String Status=Stopped


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Friday, August 14, 2015 5:49 AM

Answers

  • Get-Member list an objects members in its own order and it cannot be changed.

    [pscustomobject] maintains declared order as does [ordered]@{}

    That is just the way it is.


    \_(ツ)_/

    • Marked as answer by Satyajit321 Friday, August 14, 2015 11:19 AM
    Friday, August 14, 2015 6:29 AM
  • Hi Jrv,

    Thanks for confirming that this can't be done via Get-Member.

    Found another way, that does exactly what I was looking for.

    PS Script> $pdata[0].psobject.properties.name
    Name
    Status
    DisplayName
    PS Script> $pdatas[0].psobject.properties.name
    Status
    DisplayName
    Name

    Now what I'm wondering is how I'm suppose to learn that the object has a method or parameter called .psobject which contains lots of other stuff inside. Get-Member didn't list it. How to know what all hidden parameters it supports. Tabbing through doesn't list it.

    PS Script> $pdatas[0].psobject
    
    
    Members             : {System.String Status=Stopped, System.String DisplayName=Peer Networking Identity
                          Manager, System.String Name=p2pimsvc, string ToString()...}
    Properties          : {System.String Status=Stopped, System.String DisplayName=Peer Networking Identity
                          Manager, System.String Name=p2pimsvc}
    Methods             : {string ToString(), bool Equals(System.Object obj), int GetHashCode(), type GetType()}
    ImmediateBaseObject :
    BaseObject          :
    TypeNames           : {Selected.System.Management.Automation.PSCustomObject,
                          System.Management.Automation.PSCustomObject, System.Object}


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    • Marked as answer by Satyajit321 Friday, August 14, 2015 11:19 AM
    Friday, August 14, 2015 7:06 AM

All replies

  • Get-Member list an objects members in its own order and it cannot be changed.

    [pscustomobject] maintains declared order as does [ordered]@{}

    That is just the way it is.


    \_(ツ)_/

    • Marked as answer by Satyajit321 Friday, August 14, 2015 11:19 AM
    Friday, August 14, 2015 6:29 AM
  • Hi Jrv,

    Thanks for confirming that this can't be done via Get-Member.

    Found another way, that does exactly what I was looking for.

    PS Script> $pdata[0].psobject.properties.name
    Name
    Status
    DisplayName
    PS Script> $pdatas[0].psobject.properties.name
    Status
    DisplayName
    Name

    Now what I'm wondering is how I'm suppose to learn that the object has a method or parameter called .psobject which contains lots of other stuff inside. Get-Member didn't list it. How to know what all hidden parameters it supports. Tabbing through doesn't list it.

    PS Script> $pdatas[0].psobject
    
    
    Members             : {System.String Status=Stopped, System.String DisplayName=Peer Networking Identity
                          Manager, System.String Name=p2pimsvc, string ToString()...}
    Properties          : {System.String Status=Stopped, System.String DisplayName=Peer Networking Identity
                          Manager, System.String Name=p2pimsvc}
    Methods             : {string ToString(), bool Equals(System.Object obj), int GetHashCode(), type GetType()}
    ImmediateBaseObject :
    BaseObject          :
    TypeNames           : {Selected.System.Management.Automation.PSCustomObject,
                          System.Management.Automation.PSCustomObject, System.Object}


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    • Marked as answer by Satyajit321 Friday, August 14, 2015 11:19 AM
    Friday, August 14, 2015 7:06 AM
  • I recommend getting one of the more technical books on PowerShell and learning the details of how it works.  All of these questions are answered in those books. 


    \_(ツ)_/

    Friday, August 14, 2015 7:12 AM
  • Hi Jrv,

    Thanks, I would try to get hold of some.

    Meanwhile some research lead me to the finding of hidden data within the PSObject that too using Get-Member.

    Get-Member is awesome.

    PSObject Members

    https://msdn.microsoft.com/en-us/library/system.management.automation.psobject_members(v=vs.85).aspx

    PS Transpose> $pdata.psobject | Get-Member
    
    
       TypeName: System.Management.Automation.PSMemberSet
    
    Name                MemberType Definition
    ----                ---------- ----------
    CompareTo           Method     int CompareTo(System.O
    Copy                Method     psobject Copy()
    Equals              Method     bool Equals(System.Obj
    GetHashCode         Method     int GetHashCode()
    GetMetaObject       Method     System.Dynamic.Dynamic
    GetObjectData       Method     void GetObjectData(Sys
    GetType             Method     type GetType()
    ToString            Method     string ToString(), str
    BaseObject          Property   System.Object BaseObje
    ImmediateBaseObject Property   System.Object Immediat
    Members             Property   System.Management.Auto
    Methods             Property   System.Management.Auto
    Properties          Property   System.Management.Auto
    TypeNames           Property   System.Collections.Obj


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    • Edited by Satyajit321 Friday, August 14, 2015 11:19 AM
    Friday, August 14, 2015 11:18 AM