Skip to content

Inconsistent stringification of [pscustomobject] instances - calling .ToString() on custom objects yields the empty string #6163

Closed

Description

In the context of implicit stringification as part of expandable strings, [System.Management.Automation.PSCustomObject] instances expand to a hashtable-like representation.

By contrast, calling .ToString() explicitly yields the empty string; only .psobject.ToString() results in the same representation as when string-expanding.

Steps to reproduce

$co = [pscustomobject] @{ one = 'for the road' }
"string-expanded: [$co]"
".psobject.ToString: [{0}]" -f $co.psobject.ToString()
".ToString: [{0}]" -f $co.ToString()

Expected behavior

string-expanded: [@{one=for the road}]
.psobject.ToString: [@{one=for the road}]
.ToString: [@{one=for the road}]

All three approaches should result in the same stringification.

Actual behavior

string-expanded: [@{one=for the road}]
.psobject.ToString: [@{one=for the road}]
.ToString: []

Note how $co.ToString() returned the empty string.

Environment data

PowerShell Core v6.0.1
Windows PowerShell v5.1

Activity

  1. added
    Issue-Enhancement
    WG-Language
    on Feb 20, 2018
  2. changed the title [-]Inconsistent stringification of [pscustomobject] instances[/-] [+]Inconsistent stringification of [pscustomobject] instances - calling .ToString() on custom objects[/+] on Nov 6, 2018
  3. mklement0 commented on Dec 20, 2019

    @mklement0
    ContributorAuthor

    Just came across a real-world case where this bug can be quite confusing in this Stack Overflow question:

    The default output formatting for property values that contain [pscustomobject]s is a hashtable-like representation:

    PS> [pscustomobject] @{ prop = [pscustomobject] @{ one = 1; two = 2 } }
    
    prop
    ----
    @{one=1; two=2}   # Note the hashtable-*like* representation

    If property two is instead made to contain another [pscustomobject] instance, the bug surfaces:

    PS> [pscustomobject] @{ prop = [pscustomobject] @{ one = 1; two = [pscustomobject] @{ three = 3 } } }
    
    prop
    ----
    @{one=1; two=} # !! .two appears EMPTY now, even though it isn't.

    In the context of visually inspecting nested [pscustomobject] instances converted from JSON (see linked post), this gives the mistaken impression that data is missing.

  4. changed the title [-]Inconsistent stringification of [pscustomobject] instances - calling .ToString() on custom objects[/-] [+]Inconsistent stringification of [pscustomobject] instances - calling .ToString() on custom objects yields the empty string[/+] on Feb 7, 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. added
    Resolution-No Activity
    on Nov 16, 2023
  8. 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.

  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-EnhancementResolution-No ActivityWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions