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
Select-Object -ExpandProperty and -Property should be mutually exclusive #6161
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.
[Closed, because it is based on a misconception of how -ExpandProperty works.]
-ExpandProperty makes Select-Object return a single property value, which is mutually exclusive with -Property, which outputs a custom object with the specified properties.
Currently, -Property is quietly ignored when -ExpandProperty is also specified.
Steps to reproduce
[pscustomobject] @{ one=1; two=2; three=3 } |Select-Object-ExpandProperty one -Property three
Expected behavior
A statement-terminating error complaining about mutually exclusive parameters should be issued.
Actual behavior
1
That is, the -Property argument was silently ignored.
That is not entirely correct and making it mutually exclusive would be a breaking change.
-ExpandProperty on a Property containing an Array or single value will ignore other properties like your example.
-ExpandProperty on a Property containing a PSObject will add a row for record with the-Property list and include the expanded properties that matched to that row.
Using your example, I embed PSObject called three containing values 3 and 4 and it gets added :
[pscustomobject] @{ one=1; two=2; three= [pscustomobject] @{ three=3; four=4 } }|
Select one, two -ExpandProperty three
three four one two
---------------3412
This is very useful for flattening data from nested objects so that you have it all in the main record. For example, I took custom fields that were embedded in a ticket and expanded it this way so that the custom field would look like a regular field alongside the others.
This is one of the things I do a lot so the proposed change would break half my scripts.
The current Select-Object docs are woefully inadequate (incomplete, and partially misleading).
Here's what actually happens, and from that perspective the behavior is consistent in all 3 cases you mention:
The single property you pass to -ExpandProperty becomes the output type. That is, instances of whatever type the referenced property is are output.
Any properties you pass to -Property are added as note properties to those output-type instances.
If the -ExpandProperty property is a collection, the note properties are added to each element of that collection (and the collection is invariably converted to [object[]] on output).
Often, the output formatting may not show these note properties, which makes it look like they're not there (ignored), but they are.
In short: The real problem is one of poor documentation, and I'll add an issue in the docs repo.
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.
Activity
dragonwolf83 commentedon Feb 16, 2018
That is not entirely correct and making it mutually exclusive would be a breaking change.
-ExpandPropertyon a Property containing an Array or single value will ignore other properties like your example.-ExpandPropertyon a Property containing a PSObject will add a row for record with the-Propertylist and include the expanded properties that matched to that row.Using your example, I embed
PSObjectcalledthreecontaining values 3 and 4 and it gets added :This is very useful for flattening data from nested objects so that you have it all in the main record. For example, I took custom fields that were embedded in a ticket and expanded it this way so that the custom field would look like a regular field alongside the others.
This is one of the things I do a lot so the proposed change would break half my scripts.
mklement0 commentedon Feb 17, 2018
Thanks, Troy Campbell (@dragonwolf83) - I forgot about the many subtleties of
-ExpandProperty.The current
Select-Objectdocs are woefully inadequate (incomplete, and partially misleading).Here's what actually happens, and from that perspective the behavior is consistent in all 3 cases you mention:
The single property you pass to
-ExpandPropertybecomes the output type. That is, instances of whatever type the referenced property is are output.Any properties you pass to
-Propertyare added as note properties to those output-type instances.If the
-ExpandPropertyproperty is a collection, the note properties are added to each element of that collection (and the collection is invariably converted to[object[]]on output).Often, the output formatting may not show these note properties, which makes it look like they're not there (ignored), but they are.
In short: The real problem is one of poor documentation, and I'll add an issue in the docs repo.
mklement0 commentedon Feb 17, 2018
I've created a documentation issue: MicrosoftDocs/PowerShell-Docs#2130