X Tutup
Skip to content

Make -is [pscustomobject] and -as [pscustomobject] work meaningfully #11921

@mklement0

Description

@mklement0

Note: The following discusses the issue with respect to the -is operator, but the same applies to the
-as operator (which is currently broken even when using the full type name - see #4343).

-is [pscustomobject] (and analogously -as [pscustomobject]) should work meaningfully as follows:

# -is [pscustomobject] should only be true for true 
# [System.Management.Automation.PSCustomObject] instances
# (PSObject instances without a .NET base object)

[pscustomobject] @{} -is [pscustomobject] | Should -BeTrue # OK
42 -is [pscustomobject] | Should -BeFalse  # OK
(Get-Item /) -is [pscustomobject] | Should -BeFalse  # BROKEN
(Write-Output 42) -is [pscustomobject] | Should -BeFalse # BROKEN

Background Information

Currently, the only way to test meaningfully for whether a given value is a custom object is to do the use the full type name:

# Works, but only with the *full* type name.
[pscustomobject] @{} -is [System.Management.Automation.PSCustomObject] | Should -BeTrue
(Get-Item /) -is [System.Management.Automation.PSCustomObject] | Should -BeFalse

Surprisingly, [pscustomobject] does not work meaningfully:

# !! Returns $true, because [pscustomobject] is really [psobject]
(Get-Item /) -is [pscustomobject] 

The reason is that [pscustomobject] is unexpectedly the same as [psobject] (!), i.e., it is type accelerator for System.Management.Automation.PSObject rather than for System.Management.Automation.PSCustomObject, for historical reasons (see #4344).
Therefore, any [psobject]-wrapped .NET object returns $true for -is [pscustomobject], which is both confusing and virtually useless.

Note that there already is precedent for situationally distinguishing between [pscustomobject] and [psobject], namely in the context of custom-object literals via syntactic sugar [pscustomobject] @{ ... } (constructs a custom object), which doesn't work with [psobject] @{ ... } (constructs a hashtable and uselessly wraps it in [psobject]).

See also:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or more

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup