3

Related to this interesting question from zett42, seems like object instances with hidden or private properties could be breaking the for-display formatting when piped to Get-Member as well as Select-Object *.

Hopefully someone could shed some light on this odd behavior.

class MyBool2 {
    hidden [bool] $Value
    
    MyBool2([bool] $Value) {
        $this.Value = $Value
    }

    [string] ToString() {
        return "$($this.Value)"
    }
}

$instance = [MyBool2] $true

Once the instance has been initialized, from our console:

PS /> $instance
True

PS /> $instance | Get-Member

   TypeName: MyBool2

Name        MemberType Definition
----        ---------- ----------
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
ToString    Method     string ToString()

PS /> $instance


PS /> $instance.ToString()
True

NOTE, this behavior is only observed in PowerShell Core. Does not seem to be the case for Windows PowerShell.

GitHub Issue #17071 has been submitted for anyone wanting to jump in.

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • 2
    Very strange indeed. That smells like a bug - `Get-Member` should be observing only, without side effects. – zett42 Mar 27 '22 at 00:51
  • 1
    @zett42 it's not only `gm`, `select *` is breaking it too – Santiago Squarzon Mar 27 '22 at 00:53
  • 2
    Now the question is what [both](https://github.com/PowerShell/PowerShell/blob/7dc4587014bfa22919c933607bf564f0ba53db2e/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetMember.cs#L61) [cmdlets](https://github.com/PowerShell/PowerShell/blob/7dc4587014bfa22919c933607bf564f0ba53db2e/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs#L72) have in common that could cause this issue. – zett42 Mar 27 '22 at 00:58
  • 2
    @zett42 I think it's best to have the PS Team check on this, submitted [GitHub Issue #17071](https://github.com/PowerShell/PowerShell/issues/17071). Thanks zett – Santiago Squarzon Mar 27 '22 at 17:40

0 Answers0