Skip to content

Member enumeration malfunctions with array that contains 2 or more [pscustomobject] instances, [pscustomobject] contributes $null to the result even for non-existent properties #13752

Closed

Description

This bug also affects Windows PowerShell.

Normally, trying to access a non-existent property on an array makes member enumeration return $null.

However, if the array contains at least two [pscustomobject] instances, an array of $null values - one for each [pscustomobject] array element - is unexpectedly returned.

Update:

  • The repro steps below show the problem only indirectly.

  • The core problem is that [pscustomobject] instances - unlike instances of any other type - unexpectedly contribute $null to the result even for non-existent properties:

    • ([datetime]::Now, [regex] '.').Year -contains $null is $false, because [regex] instances have no .Year property, so they don't contribute to the result.
    • By contrast, ([datetime]::Now, [pscustomobject] @{}).Year -contains $null yields $true(!); that is, even though the [pscustomobject] instance doesn't have a .Year property, it unexpectedly contributed $null to the result.

Steps to reproduce

# OK: $null is returned, so .Count is 0
(1, [pscustomobject] @{ foo = 1 }).NoSuchProperty.Count | Should -Be 0

# !! FAILS: ($null, $null) is returned, whose .Count is 2
(1, [pscustomobject] @{ foo = 1 },  [pscustomobject] @{ foo = 2 }).NoSuchProperty.Count | Should -Be 0

Expected behavior

Both tests should succeeds

Actual behavior

The second test fails:

 Expected 0, but got 2.

Environment data

PowerShell 7.1.0-rc.1

Activity

  1. added
    Issue-Question
    on Oct 6, 2020
  2. vexx32 commented on Oct 6, 2020

    @vexx32
    Collaborator

    Hmm, interesting... judging from the behaviour with something like (1, 2, 3, 4).NoSuch.Count it seems like the difference might be that normal objects return AutomationNull.Value rather than true null for nonexistent properties, unless there's some code specifically handling cases like this somewhere.

  3. iSazonov commented on Oct 7, 2020

    @iSazonov
    Collaborator

    It's even worse:

    (1, [pscustomobject] @{ foo = 1 }, 12, [pscustomobject] @{ NoSuchProperty = 22 }).foo.Count
    2
    
  4. added
    WG-Interactive-Debugging
    WG-Engine
    Issue-Bug
    and removed
    Issue-Question
    WG-Interactive-Debugging
    on Oct 7, 2020
  5. iRon7 commented on Oct 9, 2020

    @iRon7

    It indeed appears to include $Null values for nonexistent properties:

    ConvertTo-Json (1, [pscustomobject] @{ foo = 1 }, 12, [pscustomobject] @{ NoSuchProperty = 22 }).foo
    [
      1,
      null
    ]
  6. nickkimbrough commented on Sep 22, 2021

    @nickkimbrough

    I just ran into this as well. Here is another example to reproduce:

    Set-StrictMode -Version 'Latest'
    
    $results = @(1,2) | ForEach-Object {
     [System.Collections.Generic.List[string]]$array = @()
        [PSCustomObject]@{
            Array = [string[]]$array
        }
    }
    
    # This will not throw
    foreach($result in $results) {
        $result.Array
    }
    
    # This will throw
    $results.Array

    This behavior does not throw if StrictMode is not enabled.

  7. changed the title [-]Member enumeration malfunctions with array that contains 2 or more [pscustomobject] instances[/-] [+]Member enumeration malfunctions with array that contains 2 or more [pscustomobject] instances, [pscustomobject] contributes `$null` to the result even for non-existent properties[/+] on Apr 25, 2023
  8. added
    Resolution-No Activity
    on Nov 16, 2023
  9. aolszowka commented on Nov 16, 2023

    @aolszowka

    Reproduces for me; as described:

    > $PSVersionTable
    
    Name                           Value
    ----                           -----
    PSVersion                      7.3.9
    PSEdition                      Core
    GitCommitId                    7.3.9
    OS                             Microsoft Windows 10.0.19045
    Platform                       Win32NT
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    
    > (1, [pscustomobject] @{ foo = 1 },  [pscustomobject] @{ foo = 2 }).NoSuchProperty.Count
    2
    

    (The above should be 0)

    Same deal with the repro from Ilya (@iSazonov)

    (1, [pscustomobject] @{ foo = 1 }, 12, [pscustomobject] @{ NoSuchProperty = 22 }).foo.Count
    2
    

    (The above again should be 0)

  10. surfingoldelephant commented on Dec 31, 2024

    @surfingoldelephant

    Note that two custom object instances are not required for this member-access enumeration bug to manifest.

    Just one and at least one object of any other type in the enumerated collection is enough.

    # Name exists on at least one object, so $null values should *not* be returned.
    # The single custom object contributes $null to the output.
    ConvertTo-Json ((Get-Process -Id $PID), [pscustomobject] @{ Foo = 'Foo'}).Name
    # [
    #   "pwsh",
    #   null
    # ]
    
    # Without custom object involvement, behavior is expected:
    ConvertTo-Json ((Get-Process -Id $PID), 'foo').Name
    # "pwsh"
  11. surfingoldelephant commented on Jan 18, 2025

    @surfingoldelephant

    Here's another manifestation of this issue that doesn't involve a custom object at all.

    Add-Type -TypeDefinition @'
    using System;
    
    public class Class1 {
        public string Foo { get { throw new Exception("Error"); } }
    }
    '@
    
    ConvertTo-Json ([Class1]::new(), [Class1]::new()).Foo
    # [
    #   null,
    #   null
    # ]

    Each property getter exception contributes a $null value to the result.

    It's worth noting that while using getter exceptions is not recommended, it's still used in practice, including within the PowerShell source code.

  12. x5i4k4 commented on Jan 14, 2026

    @x5i4k4

    Please reopen this issue, it can be really confusing to resolve when dealing with parsed json list for example, it became a penalty for people have the right understanding of group operator

    (Get-Content ./list.json |  ConvertFrom-Json)."" -is [array] # true
    (Get-Content ./list.json |  ConvertFrom-Json)."" | foreach { $_ -eq $null } # all true, array of nulls
  13. iRon7 commented on Jan 15, 2026

    @iRon7
  14. surfingoldelephant commented on Jan 15, 2026

    @surfingoldelephant

    you will need to combine the lines (- join [Environment]::NewLine) first or load the whole content in once using the -Raw switch

    -Raw in this case may be recommended, but isn't mandatory. ConvertFrom-Json accumulates pipeline input, so piping individual strings that collectively form valid JSON content works just fine.

    $tmp = [IO.Path]::GetTempFileName()
    Set-Content -LiteralPath $tmp -Value @'
    [
        {
            "Foo": 1
        },
        {
            "Foo": 2
        }
    ]
    '@
    
    ($result = Get-Content -LiteralPath $tmp | ConvertFrom-Json)
    
    # Foo
    # ---
    #   1
    #   2
    
    $result.Count # 2

    Regarding "", I suspect it's being used just as an arbitrary way of provoking the issue. It could be replaced with any other member name in this example.

    ConvertTo-Json $result.""
    
    # [
    #   null,
    #   null
    # ]
    
    ConvertTo-Json $result.Bar
    
    # [
    #   null,
    #   null
    # ]

    (ConvertTo-Json is used to visually show how each custom object contributes a $null value to member-access enumeration output.)

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-BugResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions