Skip to content

Select-Object -ExpandProperty hides ETS instance members #7937

Closed

Description

Follow-up from #7768

If you use -ExpandProperty with a property whose value happens to be an object with instance ETS members (ETS members added with -Add-Member), these members are no longer present in the value returned.

Note: The behavior only surfaces if the object is not an instance of [psobject] - that is, if it is neither a true custom object ("property bag") nor an incidental [psobject] wrapper (see #5579).

PetSerAl discovered this problem in the context of #7768 and explains it as follows:

In v2 extra properties were linked to PSObject wrapper. So two PSObjects wrapping the same underlying object can have different set of extra properties. In v3 properties now linked to underlying object (with some exceptions), but not to PSObject wrapper. But in the process PowerShell devs decided to keep/grant Select-Object -ExpandProperty ability to create independent PSObject wrappers, which links properties to themselves rather than to underlying objects.
[...]
Although, I do not think Select-Object should really do this. I do not see reasons why it use/have this ability in the first place (compatibility? maybe).

A fix for this issue - to no longer attach the ETS members to the incidental wrapper and instead use the resurrection tables - should also fix the problem described in #12411.

Steps to reproduce

$v = [datetime]::now; $v | Add-Member myProp myPropValue
"[$($v.myProp)]"
'---'
# This should retrieve the original $v value, via Select-Object -ExpandProperty
 $vToo = [pscustomobject] @{ prop = $v } | Select-Object -ExpandProperty prop
"[$($vToo.myProp)]"

Expected behavior

[myPropValue]
---
[myPropValue]

That is, the .myProp instance member should still be present.

Actual behavior

[myPropValue]
---
[]

That is, the .myProp instance member is no longer present.

What presumably happens is that a [psobject] wrapper is created around the output object with storeTypeNameAndInstanceMembersLocally set to $true, which makes PowerShell look for instance members only on the wrapper object itself, effectively eclipsing any preexisting instance members associated with the wrapped object via the resurrection tables.

Environment data

PowerShell Core v6.1.0

Activity

  1. added
    Issue-Discussion
    on Oct 3, 2018
  2. changed the title [-]Select-Object -ExpandProperty quietly discards instance members[/-] [+]Select-Object -ExpandProperty quietly discards ETS instance members[/+] on Nov 19, 2019
  3. changed the title [-]Select-Object -ExpandProperty quietly discards ETS instance members[/-] [+]Select-Object -ExpandProperty hides ETS instance members[/+] on Apr 21, 2020
  4. added
    Issue-Bug
    on Apr 22, 2020
  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  8. added
    Resolution-No Activity
    on Nov 16, 2023
  9. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue-DiscussionResolution-No ActivityWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions