Skip to content

Select-Object modifies input objects when using both -ExpandProperty and -Property #7768

@bstrautin

Description

@bstrautin

Select-Object should never modify its input objects, but it does, when using both the -ExpandProperty and -Property parameters.

This behavior is documented, but wrong.

Steps to reproduce

$NestedObjects =
  [pscustomobject]@{a=1; b=[pscustomobject]@{c=2}},
  [pscustomobject]@{a=3; b=[pscustomobject]@{c=4}}

$NestedObjects | Out-Host
$NestedObjects | Select-Object a -ExpandProperty b | Out-Host

$NestedObjects | Out-Host
$NestedObjects | Select-Object a -ExpandProperty b | Out-Host

Expected behavior

Re-running the same Select-Object statement should produce the same output, and should not modify its input objects, so the example code should produce the following output:

a b
- -
1 @{c=2}
3 @{c=4}



c a
- -
2 1
4 3


a b
- -
1 @{c=2}
3 @{c=4}



c a
- -
2 1
4 3


Actual behavior

Instead of creating new objects with the appropriate fields, Select-Object modifies the objects specified by -ExpandProperty, which causes an error the second time the same code runs:

a b
- -
1 @{c=2}
3 @{c=4}



c a
- -
2 1
4 3



a b
- -
1 @{c=2; a=1}
3 @{c=4; a=3}


Select-Object : The property cannot be processed because the property "a" already exists.
At line:9 char:18
+ $NestedObjects | Select-Object a -ExpandProperty b | Out-Host
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (@{a=1; b=}:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : AlreadyExistingUserSpecifiedPropertyExpand,Microsoft.PowerShell.Commands.SelectObjectCommand


Select-Object : The property cannot be processed because the property "a" already exists.
At line:9 char:18
+ $NestedObjects | Select-Object a -ExpandProperty b | Out-Host
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (@{a=3; b=}:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : AlreadyExistingUserSpecifiedPropertyExpand,Microsoft.PowerShell.Commands.SelectObjectCommand

c a
- -
2 1
4 3

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.1
PSEdition                      Core
GitCommitId                    v6.0.1
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions