-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Labels
Breaking-Changebreaking change that may affect usersbreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionPS-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 aideally 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 moreIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
Description
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-HostExpected 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.0hilari0n
Metadata
Metadata
Assignees
Labels
Breaking-Changebreaking change that may affect usersbreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionPS-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 aideally 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 moreIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors