-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
I see novices posting variations of this over and over on the internet:
I have this code:
get-thing | select namebut 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 Nameshould acted as if you specified-ExpandProperty Nameand 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|% nameor the awkward( ).Name, then simplyselect-object -property namewould 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?)
- 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
- 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-valueorselect-propertyvalueorget-objectpropertyor etc.