Skip to content

Select-Object -ExpandProperty and -Property should be mutually exclusive #6161

Closed

Description

[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.

Environment data

PowerShell Core v6.0.1
Windows PowerShell v5.1

Activity

  1. dragonwolf83 commented on Feb 16, 2018

    @dragonwolf83

    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
    ----- ---- --- ---
        3    4   1   2

    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.

  2. mklement0 commented on Feb 17, 2018

    @mklement0
    ContributorAuthor

    Thanks, Troy Campbell (@dragonwolf83) - I forgot about the many subtleties of -ExpandProperty.

    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.

  3. mklement0 commented on Feb 17, 2018

    @mklement0
    ContributorAuthor

    I've created a documentation issue: MicrosoftDocs/PowerShell-Docs#2130

  4. added
    Issue-Question
    Resolution-Answered
    on Mar 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-QuestionResolution-Answered

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions