Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Hidden-only class loses ToString() formatting after Get-Member inspection #25832

@GregoireLD

Description

@GregoireLD

Prerequisites

Steps to reproduce

If you define a class with only hidden properties and an overload to the ToString() method, displaying the object falls back to the ToString() method, as expected.

But a call to Get-Member on such an object, forces a full ETS refresh: the formatter now believes the type has visible members and switches to the list renderer, resulting in blank lines (since actually all members are hidden) instead of the expected ToString().

I found this issue was first noticed in issue #17071 but I just ran into it myself when coding a new class in PowerShell. It's still occurring on PowerShell 7.5.2 .

Code snippet

1. Minimalistic sample class

class CustomClass {
    hidden [string] $myString
    CustomClass([string] $newVal) { $this.myString = $newVal }
    [string] ToString() { return $this.myString }
}

$myObject = [CustomClass]"Test"

# 1. Normal display uses ToString()
$myObject

# 2. Inspect the object
$myObject | Get-Member

# 3. Display again – blank line appears
$myObject

Expected behavior

PS /> $myObject = [CustomClass]"Test"
PS /> $myObject
Test
PS /> $myObject | Get-Member

   TypeName: CustomClass

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

PS /> $myObject
Test

Actual behavior

PS /> $myObject = [CustomClass]"Test"
PS /> $myObject
Test
PS /> $myObject | Get-Member

   TypeName: CustomClass

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

PS /> $myObject

Error details

Environment data

PS /> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.5.2
PSEdition                      Core
GitCommitId                    7.5.2
OS                             Darwin 24.6.0 Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:29 PDT 2025; root:xnu-11417.…
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.WG-Enginecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working Group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions