Skip to content

Request / Discussion: Select-Object with one property causes many problems for beginners and has few legitimate use cases for anyone. Can that experience be improved for beginners? #5237

@HumanEquivalentUnit

Description

@HumanEquivalentUnit

I see novices posting variations of this over and over on the internet:

I have this code: get-thing | select name but why are there @{} in my output and how do I get rid of them? Why doesn't the output equal -eq 'user1' ? Why doesn't my string split/replace work on it?

And they're too novice to understand the object pipeline well and know what it means to have an object with one property. They want the name, that looks like the way to do it, it looks like it works, but it doesn't actually do what they want.

There's lots of people tripping over this basically-useless edge-case, over and over and over on their way to learning PowerShell well.

Can we come up with a way to smooth that for beginners before they know enough to avoid it? (i.e. putting a rule in PSScriptAnalyzer would be too late to help).

It's my suspicion that selecting one thing, select-object -property OnePropertyHere , is an anti-pattern, and has so few legitimate use cases that they would have to be contrived to show what it does.

Ideas:

  • Selecting one thing should throw an exception "you probably don't want to do that, go read a tutorial on the object pipeline".
  • Selecting one thing should output a warning to the verbose stream.
  • Select Name should acted as if you specified -ExpandProperty Name and output a value, not an object. This would be a breaking change, but..
    • Then it would be useful for people who do know about the object pipeline as well, who still (I speculate) have few real uses for selecting an object with one property. Where the alternatives are currently the laborious -ExpandProperty, the convenient but ugly |% name or the awkward ( ).Name, then simply select-object -property name would do what you most likely actually want it to do. (Downside, how would you then specify if you do want an object with one property?)
  • There should be another cmdlet for this use case which is clearly named to show that it won't output an object, because even "select an object and expand a property so the output is not necessarily an object" is a bit awkward when you stare at it for a while. e.g. select-value or select-propertyvalue or get-objectproperty or etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or more

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions