Repository navigation
Open
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
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
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
$myObjectExpected 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
TestActual 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 /> $myObjectError 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.0Visuals
No response
Reacted by Michael Klement
Metadata
Metadata
Assignees
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
Activity
similar-issues-ai commentedon Aug 6, 2025
We've found some similar issues:
If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.
GregoireLD commentedon Aug 8, 2025
I might be completely wrong, and I can't currently perform much compiling, but after staring the code a lot, I think I might've found the culprit.
PowerShell/src/System.Management.Automation/FormatAndOutput/common/FormatViewManager.cs
Lines 506 to 511 in f4a452e
Here we treat the object as a scalar but only if either, it's a standard type (Which my object is never, so always false in my case) or, if HasNonRemotingProperties is false.
I think before the introspection it's indeed false, but after the ETS checks on all hidden properties, it must switch to true, skip the scalar treatment, and reach the empty list or table treatment bellow.
I'm not sure about modifying the HasNonRemotingProperties itself, but maybe having no visible property could be a third valid reason to get the scalar treatment. I don't feel fluent enough in C# to tackle this myself, but I hope this helps.
mklement0 commentedon Aug 10, 2025
GregoireLD, I haven't looked too deeply, but I don't think this is where the problem lies:
DefaultScalarTypes.IsTypeInList's behavior is static, and would never returntruefor you customclassWhile it's not readily obvious whether
HasNonRemotingPropertiestakes hidden properties into account, note that the problem also surfaces with property-less types, for which it should returnfalseeither way.A simple repro:
Interestingly, piping to
Format-List -Forcesurfaces the bug, too.GregoireLD commentedon Aug 12, 2025
Michael Klement (@mklement0) indeed I agree
Yeah that's my understanding too
Interesting, but I'm wondering if property-less types in that context could still get technical properties attached from calling commands like Get-Member.
My line of thought is that I can't find any other places where the output don't get wrapped in new-lines :
My custom type is outputting a single string from the ToString method.
that's why I was searching for a place that would fit this: depending on this single "if" statement, we split between a GenerateOutOfBandObjectAsToString(so) with no extra lines, or the usual object output with wrapping lines. This would also match why forcing a "Format-List -Force" also triggers the bug as you noted.
My other side-note though is, since property-less types are bound to output using (theirs or inherited) ToString() method, letting them go through the same path as simple strings could make sense (which seems to be the default, pre-bug behavior anyway) -> adding a third condition in this if statement: 1. DefaultScalarTypes, or 2. no HasNonRemotingProperties, or 3. property-less object .
As stated before, I'm still pretty new to the PowerShell code-base so I might be completely off-track tho.
GregoireLD commentedon Aug 12, 2025
I can confirm this is where it happens: (I know my probing was a bit ... brutal)
when replacing line 510 of FormatViewManager.cs with
any subsequent call to my object display "Test" as a Scalar (with no extra lines), and after triggering the bug, it's back to my usual displaying of empty lines. So yes I'm pretty sure this is where it happens.
One extra issue that possibly lies in here too, If I specify a "format.ps1xml" file to my class:
mklement0 commentedon Aug 12, 2025
Thanks for digging deeper.
So the implication seems to be that the return value of
!HasNonRemotingProperties(so)changes after having calledGet-MemberorFormat-List -Force- for reasons unknown to me, given that piping toGet-Member -Forcedoesn't reveal any (unexpectedly) attached new properties.Note that the method name
HasNonRemotingProperties()implies that it should also returnfalsefor property-less instances, so there should be no need to amend the conditional.In the context of for-display formatting, the semantics of
HasNonRemotingProperties()should really beHasNonHiddenNonRemotingProperties(), but I haven't dug deep enough to verify that hidden properties are excluded.GregoireLD commentedon Aug 12, 2025
I'm slowly inching my way towards understanding this ... the split is not exactly where I initially thought.
The split appears to occurs on line 510 indeed, but if it doesn't branch here, it doesn't go all the way to the end of the method, it's caught right bellow with the
if (!useToStringFallback), immediately returning nullI'm currently testing what would be the consequences of flipping the useToStringFallback flag here
GregoireLD commentedon Aug 12, 2025
Unfortunately switching the
useToStringFallbackflag messes with other outputs.Yet, I have found the following minimal changes to fix the current behavior with no visible downsides so far:
FormatViewManager.cs:ToStringcall (similar to the current pre-bug behavior)|| (new PSPropertyExpression("*").ResolveNames(so).Count == 0)to the current if check at line 508/509new PSPropertyExpression("*").ResolveNames(so)... == 0check at lines 518/522This:
useToStringFallback, because this one will trigger a return null anywayToString()content of the object instead of null if the object has no visible propertiesAt first glance, It could be said this introduce a behavior change since we return a
ToStringwithout checking theuseToStringFallbackflag, but currently it's the other way around : after the bug gets triggered, the code will return null instead of the ToString only when we allow for auseToStringFallback(and before the bug, we get aToStringanyway too).Added "Bonus" of some kind,
Format-List -Forcestays the same before and after triggering, and is empty, similarly to other raw Scalars we can encounter (42 | Format-List -Forceis empty too) but does not induce any output issue anymore.So far I haven't run into any issues after making these changes, but I'd like to have someone more experienced to run some tests too before I even think of making a PR about it 😊
GregoireLD commentedon Aug 12, 2025
I noticed one thing this change makes (but I think it's a good thing), if you have a format.ps1xml associated with your class:
currently:
after this fix:
I was perplexed as to how I could use the format.ps1xml file to specify a simple scalar output, and it seems there is no way to do that. This fix offers a direct solution, any property-less object is by default treated as a scalar, and the format.ps1xml is used automatically on any property-equipped object.
GregoireLD commentedon Aug 13, 2025
Here is the consolidated changes I'm suggesting :
GregoireLD:PowerShell-PropertylessFix
GregoireLD commentedon Aug 13, 2025
To double-check my idea, I tracked when was the change to
HasNonRemotingProperties()made.I discovered that prior to this change, on Commit 42c289f, the test was indeed against the now defunct
IsPropertyLessObject(so)but was changed apparently for performance reasons.Commit 42c289f Compare
As such, I feel more and more confident about my change, given that performance-wise, the lazy evaluation means that:
HasNonRemotingProperties()is true it won't go any further, and(new PSPropertyExpression("*").ResolveNames(so).Count == 0)then gets evaluated here, restoring the original aim of making sure property-less objects returns their ToString() content and not a null value.mklement0 commentedon Aug 13, 2025
I haven't dug deeper, but I'm thinking a fix would require two things:
Make
HasNonRemotingProperties()invariably exclude hidden properties or, if it is used elsewhere where hidden properties should be reported, make it accept aMshMemberMatchOptionsargument and passNone- there should then be no need for(new PSPropertyExpression("*").ResolveNames(so).Count == 0)Fix the fact that
Get-MemberandFormat-List -Forcechange the behavior of existing instances.I've checked the Windows PowerShell behavior, and it is correct for both property-less and hidden-properties-only types.
Perhaps Staffan Gustafsson (@powercode), who authored the PR that may have introduced the bug, can share his thoughts.
GregoireLD commentedon Aug 14, 2025
If I understand correctly, current
HasNonRemotingProperties()is a lambda-expression that relies on theFirstOrDefault(predicate)function, predicate being the filtered name tests inIsNotRemotingProperty(string name)I don't see how to make a filter like FirstOrDefault become FirstVisibleOrDefault without rebuilding it from scratch (but I'm a C# newbie so maybe that's on me).
Prior to this change, the check we are talking about was referring to the following function:
I which we see the end part is all that subsist nowadays in the form of the current
HasNonRemotingProperties()function.If I understand correctly, the old test was retrieving all properties (supposedly visible), and excluding RemotingProperties from the count to get a final visibility check.
One (easy, in the sense I think I could do it) way could be to replace this current call to
HasNonRemotingProperties()in the "if" with a modernizedIsPropertyLessObject(PSObject so), checking both for the HasNonRemotingProperties() and counting visible properties returning a directly usable single boolean value.GregoireLD commentedon Aug 14, 2025
Here is my findings so far:
IsPropertyLessObject(PSObject so)"as-is" (minus the no-longer-existing SourceLength RemotingConstant) fixes the issue, restore the initial behavior, fully matching what is expected, and what we see on Windows PowerShellIsNotRemotingPropertymethodIsPropertyLessObject(PSObject so), leveraging the currentIsNotRemotingPropertymethodProposed modification
mklement0 commentedon Aug 15, 2025
Thanks for all your efforts, GregoireLD
It still seems to me that two aspects need to be expressed explicitly:
Get-Member/Format-List -Forceunexpectedly change the current behavior, and whether other use cases may be affected too.Once we have clarity and agreement on how to proceed, a PR can be created.
GregoireLD commentedon Aug 15, 2025
Michael Klement (@mklement0) true, I've build a few more sets to tests this but as far as I understand:
AssociationManager.ExpandAll(so)/allProperties.CountI currently use in my newIsPropertyLessObject(PSObject so)function does not get fooled by hidden properties and only return true is there is valid visible non-remote properties in said object.Get-Memberand such, but in the way the LambdaHasNonRemotingProperties(so)is parsing the object :HasNonRemotingProperties(so)apparently saw the static "PSTypeNames" class member and mistakenly confronted it to the Non-Remoting list. My understanding is that the way this lambda evaluates the object was too wide and encompasses more than just properties belonging to the object, but also "wrapping" or additional "tooling" properties, causing issues once the object gets populated with non-properties class members -> That explains why callingIsNotRemotingProperty(property)explicitly solely on each visible properties solves it, it doesn't wrongly mistakes non-property class members as valid properties.HasNonRemotingProperties(so)is only used at one other location, and in the exact same context of filtering PropertyLessObject : OutGridViewCommand.cs, which displays the same issue. I just replaced it in the exact same way for consistency and I'm currently rebuilding this new version to test it.Let me know if this shed some more light on all of this
Current test
mklement0 commentedon Aug 15, 2025
That sounds promising, but I still don't understand why the symptom only surfaces after having called
Get-MemberorFormat-List -Force.GregoireLD commentedon Aug 15, 2025
This is what I will try to understand next, but for what it's worth, I indeed saw that .pstypenames is a hidden "CodeProperty" member that does exist from the get go (That's the one I saw appear when inspecting the former HasNonRemotingProperties() method).
So I get the feeling it's a combination of caching the existence of a hidden property, and going through a HasNonRemotingProperties() non-visibility aware that wrongly returns that some properties must be visible.
I will see if I find any wrongdoing on the Get-Member side, but the fact that the object-side fixes it might indicate the issue was only there.
GregoireLD commentedon Aug 16, 2025
I'm pulling at straws here as things are getting a bit hard to follow for a newcomer like me, but I think I got the big picture :
PSObject.GetMemberCollectionExpandAll(so)but ends up in the same placeGetMemberCollectionfunction triggers a set of delegates for the Extended / Adapted / Base viewsPSMemberInfoInternalCollection<T> ret = msjObj.InternalAdapter.BaseGetMembers<T>(msjObj._immediateBaseObject);reflects / adapts the object and returns all properties, including hidden ones, but here it's fine I think. and PSMemberInfoIntegratingCollection collates all newly discovered properties.As far as I understand, everything else is fine here, just that the object itself "knows" which properties it owns and which are visibles, and relying solely on the HasNonRemotingProperties(so) delegate included reflected properties that should never be included here in the first place.
I think no changes are required elsewhere, just in the FormatViewManager (and the same change in OutGridViewCommand)
GregoireLD commentedon Aug 16, 2025
Michael Klement (@mklement0) Do you think this could be ready to propose as a PR for closer inspection ?
Current Status
mklement0 commentedon Aug 16, 2025
I'd say so, yes.
One aspect left to address is that the original PR's motivation was a performance improvement, so it would be good to understand the performance impact of your proposed fix.
A fix is undoubtedly necessary, but perhaps there is a way to improve its performance through caching - I personally cannot speak to that (I don't know the cost of repeated
AssociationManager.ExpandAll(so)calls).Again, thanks for your efforts.
GregoireLD commentedon Aug 16, 2025
True.
What I hope is that, this since this will only impact objects without associated views, or propertyless objects when called without an explicit ToString() invocation, the impact should be fairly limited. And the optimisation scope of the previous PR was fairly large, this will only undermine one of the many optimisations.
PR Done, thanks for all your help Michael Klement (@mklement0) , I'm pretty new to this 😊
I don't know the process from here (not that I knew much before here either 😅), feel free to let me know if I should do anything else
GregoireLD commentedon Sep 18, 2025
Just to link this to the PR to this issue :
PR 25871
As I'm not yet accustomed to this repo's procedures, feel free to notify me if anything is needed from me.
iSazonov commentedon Nov 22, 2025
Michael Klement (@mklement0) I guess Engine does "full psobject init" and as result add
pstypenamesproperty.powercode commentedon Apr 6, 2026
Unfortunately, I cannot help you with any insights. I took me a while to even remember that I had made that PR.
As I recall, the focus was to not allocate as many objects for formatting and optimize how PSObject allocated when we knew up front how many properties it would have.
I would suspect that the focus of both the implementation and the review was centered around those changes, and that edge cases weren't properly considered.