locked
Get the PSStandardMembers for any PowerShell Object RRS feed

  • Question

  • Good evening

    it looks like there is no answer to this question: If we call:
    $Obj | Select-Object
    then, Select-Object automatically detects the object properties, which have to be printed by default.

    How can we get the same list of object properties, which have to be printed by default, which is used by Select-Object?

    One (bad) solution would be to catch the string result of $Obj | Select-Object and then parse the string ;-)

    Thanks a lot for any idea,
    kind regards,
    Thomas


    • Moved by Bill_Stewart Thursday, December 13, 2018 3:29 PM Unreadable posts
    Thursday, September 20, 2018 9:39 PM

All replies

  • You would have to look in the format file for the returned object type.


    \_(ツ)_/

    Thursday, September 20, 2018 10:08 PM
  • Thanks a lot for your answer.

    But, of course, if we need <g class="gr_ gr_22 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="22" id="22">exact</g> the same result that Select-Object produces,
    then it's not a good idea to reinvent the wheel and redevelop the code that already exists.

    Additionally, handling the format file is very tricky and error-prone.

    Therefore, we search for something like

    $obj.PSObject.PSStandardMembers | Select Names

    ... which always works. As Select-Object does.


    • Edited by Jehoschua Thursday, September 20, 2018 10:38 PM Annyoing M$ Editor adds formatting into text. It's a shame!
    Thursday, September 20, 2018 10:36 PM
  • No. But you can do this for standard objects that have a format file reference:

    $obj.PsStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames

    Example:

    PS D:\scripts> $obj = Get-Service spooler
    PS D:\scripts> $obj.PsStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
    Status
    Name
    DisplayName
    PS D:\scripts>


    \_(ツ)_/


    • Edited by jrv Thursday, September 20, 2018 10:45 PM
    Thursday, September 20, 2018 10:44 PM
  • Thank you, <g class="gr_ gr_18 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="18" id="18">jrv</g>, for your help,

    unfortunately, it does not answer the question / solve the problem.

    Select-Object is always able to get the properties which are printed by default,
    your idea does not work for a generic Pipeline-Processing function, e.g. if we get FileSystem Objects:

    $obj = Get-Item C:\
    $obj.PsStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
    # returns $null.
    # Will never happen in Select-Object, therefore it's no answer / solution for the quesion :-(
    





    • Edited by Jehoschua Friday, September 21, 2018 8:13 AM Annoying HTML-'Editor'
    Friday, September 21, 2018 8:12 AM
  • Which is what I tried to warn you about.  The only accurate method is to get the properties from the format file.


    \_(ツ)_/

    Friday, September 21, 2018 8:19 AM
  • I'm very sorry, but I think you're wrong <g class="gr_ gr_382 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Punctuation only-del replaceWithoutSep" data-gr-id="382" id="382">:-(,</g> because

    • the format files don't exist for each possible object, therefore, we must use at least one additional logic 
    • I guess that Select-Object detects Views, too. Therefore, we probably would have to parse the View definitions, too.
    • Even if the format files are the only needed source: they sometimes have more than one set of default parameters, so we have - once more - to copy the selection logic of Select-Object

    There is a light: I've just found this function:

    internal IList<string> BuildPropertyNames(PSObject source) {}

    Source: https://github.com/PowerShell/PowerShell/blob/master/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs

    It looks like it does the same thing as Select-Object is doing. It's very sad that it's an internal function and to create a copy of a c# class in PowerShell is tricky.

    Friday, September 21, 2018 8:33 AM
  • That gets all property names on an object.  It is done in PowerShell like this:

    $obj.PsObject.Properties | select name

    or

    $obj | Get-Member -MemberType Property


    \_(ツ)_/

    Friday, September 21, 2018 8:44 AM
  • Thanks a lot for your hint!, so I can stop going this wrong way.

    I guess I have to study the code of Select-Object, maybe they have something like Get-DefaultProperties() :-)

    Friday, September 21, 2018 10:55 AM
  • Thanks a lot for your hint!, so I can stop going this wrong way.

    I guess I have to study the code of Select-Object, maybe they have something like Get-DefaultProperties() :-)

    What is it that you mean by "DefaultProperties".

    Get-Member can get any kind of member that exists.  I think part of your problem is that you are over-thinking this. Start by stating "why" you need to do this.


    \_(ツ)_/

    Friday, September 21, 2018 11:39 AM

  • What is it that you mean by "DefaultProperties".


    I still talk about the original question:

    Select-Object automatically detects the object properties, which have to be printed by default

    » We would like to process the objects in the Pipeline and we need to detect which object properties are printed by default (if the user did not have defined the list of Properties which should be processed), e.g.

    # Here,  OurOwnCommand will detect which properties are displayed by default
    # for the Objects in the Pipeline
    # » we have to use the same Properties which Select-Object would display
    Get-ChildItem | OurOwnCommand
    
    # Here, OurOwnCommand would just process the
    # Name and Size Properties of the Objects in the Pipeline:
    Get-ChildItem | Select-Object Name, Size | OurOwnCommand




    • Edited by Jehoschua Friday, September 21, 2018 12:06 PM
    Friday, September 21, 2018 12:05 PM

  • Select-Object automatically detects the object properties, which have to be printed by default


    Not true. The object properties are determined by the format file definition for the object. Even if you do not use "Select-Object" you will get only the properties specified in the default view (display) of the object.   There is no such thing as default properties.

    See: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_format.ps1xml?view=powershell-6

    All "properties" collections of an object are based on the object type selected.  The object. The PsObject.  The PsAdapted object.  The PsBase object.

    For objects with format definitions we also have the "DefaultDisplayPropertySet" which is defined by the format file.

    Once you sort out how this works you will better understand how to manipulate objects in a pipeline.
    Again, you have to clearly state why you need to do this.


    \_(ツ)_/

    Friday, September 21, 2018 12:16 PM