Skip to content

Unexpected behavior of array-valued calculated properties added to a custom object with Select-Object #3163

@mklement0

Description

@mklement0

[Superseded by #5579]

Steps to reproduce

'' | Select-Object @{ n='prop'; e={ 1, 2 } } | ConvertTo-Json

Expected behavior

{
    "prop":  [
                 1,
                 2
             ]
}

Actual behavior

Update: This symptom no longer happens in PowerShell Core due to this fix, but it can still be observed in Windows PowerShell; the symptom illustrates that the Select-Object way of creating the array-valued property differs from the seemingly equivalent [pscustomobject] @{ .... } way, which is both unexpected and could cause problems in other contexts.

{
    "prop":  {
                 "value":  [
                               1,
                               2
                           ],
                 "Count":  2
             }
}

Note that what should be an equivalent command:

[pscustomobject] @{ prop = 1, 2 } | ConvertTo-Json    

does produce the expected output.

An unexpected difference in the resulting array-valued property can also be observed via Get-Member:

(Get-Member prop -InputObject (New-Object pscustomobject |Select-Object @{ n='prop'; e={  1, 2  } })).Definition

yields

System.Object[] prop=1 2    

whereas

(Get-Member prop -InputObject ([pscustomobject] @{ prop = 1, 2 })).Definition

yields

Object[] prop=System.Object[]

For more information and a workaround, see http://stackoverflow.com/a/38212718/45375

Environment data

PowerShell v6.0.0-alpha (v6.0.0-alpha.15) on Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64
PowerShell v5.1.14393.693 on Microsoft Windows 10 Pro (64-bit; v10.0.14393)

Metadata

Metadata

Assignees

No one assigned

    Labels

    WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions