You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Member enumeration malfunctions with array that contains 2 or more [pscustomobject] instances, [pscustomobject] contributes $null to the result even for non-existent properties #13752
Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
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
Reacted by Rain Sallow (/u/ta11ow), iRon7, Ace Olszowka, Nick Kimbrough and sharpchen
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.
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 throwforeach($resultin$results) {
$result.Array
}
# This will throw$results.Array
This behavior does not throw if StrictMode is not enabled.
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
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"
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
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-Jsonaccumulates pipeline input, so piping individual strings that collectively form valid JSON content works just fine.
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.
Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
vexx32 commentedon Oct 6, 2020
Hmm, interesting... judging from the behaviour with something like
(1, 2, 3, 4).NoSuch.Countit 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.iSazonov commentedon Oct 7, 2020
It's even worse:
iRon7 commentedon Oct 9, 2020
It indeed appears to include
$Nullvalues for nonexistent properties:nickkimbrough commentedon Sep 22, 2021
I just ran into this as well. Here is another example to reproduce:
This behavior does not throw if StrictMode is not enabled.
[-]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[/+]aolszowka commentedon Nov 16, 2023
Reproduces for me; as described:
(The above should be 0)
Same deal with the repro from Ilya (@iSazonov)
(The above again should be 0)
surfingoldelephant commentedon Dec 31, 2024
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.
surfingoldelephant commentedon Jan 18, 2025
Here's another manifestation of this issue that doesn't involve a custom object at all.
Each property getter exception contributes a
$nullvalue 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.
x5i4k4 commentedon Jan 14, 2026
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
iRon7 commentedon Jan 15, 2026
surfingoldelephant commentedon Jan 15, 2026
-Rawin this case may be recommended, but isn't mandatory.ConvertFrom-Jsonaccumulates pipeline input, so piping individual strings that collectively form valid JSON content works just fine.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-Jsonis used to visually show how each custom object contributes a$nullvalue to member-access enumeration output.)