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

Closed

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.

Activity

  1. iSazonov commented on Oct 26, 2017

    @iSazonov
    Collaborator

    I wonder why a beginner don't read a cmdlet description and examples.

  2. added
    Issue-Question
    on Oct 26, 2017
  3. markekraus commented on Oct 26, 2017

    @markekraus
    Contributor

    I think changing select-object would just move the problem you are trying to address to another cmdlet. The reason why this trips novices up is that the language is object-centric. At some point a novice is going to need to learn that. They just happen to experience it with select-object first because i is a common cmdletd.

    It's my suspicion that selecting one thing, select-object -property OnePropertyHere , is an anti-pattern,

    It's not, really. You may have an object where only one of the properties is useful. The objects generated by this may be used as seed objects where other properties are added in the pipeline. Or you may be passing an object to a command that takes more than one of your source objects' properties as a ValueFromPipelineByPropertyName but only one is correct in the context. or you may simpley need to create a single column CSV with a header to import into an external system (Yammer takes a few single column CSV's, for example)

    Selecting one thing should throw an exception "you probably don't want to do that, go read a tutorial on the object pipeline".

    Definitely not for the reason mentioned above.

    Selecting one thing should output a warning to the verbose stream.

    I don't think this one is a good idea either.

    Select Name should acted as if you specified -ExpandProperty Name

    That would get confusing with a difference in operation between Select-Object Porp1 and Select-Object Prop1, Prop2.

    There should be another cmdlet for this use case

    I don't think it's worth adding a completely other cmdlet for this.

    select an object and expand a property so the output is not necessarily an object

    Where do you see that?

    This is probably something that is better fixed with documentation. Perhaps adding an example of this as common gotcha would be a good start. The Export-Csv cmdlet has an example of avoiding the mangling of objects caused by passing objects to Format-Table first. i suspect any "fix" done in Select-Object would just push that initial exposure to objects on to another cmdlet.

  4. RichardSiddaway commented on Oct 26, 2017

    @RichardSiddaway

    I can think of a number of reasons for selecting a single property that are extremely valid and don't see any reason to change the current behaviour of select-object

  5. HumanEquivalentUnit commented on Oct 26, 2017

    @HumanEquivalentUnit
    ContributorAuthor

    RichardSiddaway I cite your own blog post where you say "One thing that seems to keep causing confusion is using Select-Object to pick off one or more properties from a set of objects", and you blog that it "keeps" causing confusion 9 years after you started blogging about PowerShell.

    You don't see any reason to try and do anything to improve it?

  6. RichardSiddaway commented on Oct 26, 2017

    @RichardSiddaway

    It's not a problem that technology can fix. Its an education problem. I've seen many people jump into using PowerShell, and other technologies, and make assumptions about how they work or just don't bother to discover how they work. This is where many of the problem you cite come from.

    Changing the behaviour of select-object in the way you advocate isn't going to solve those problems as you'll just be moving the problem area to another cmdlet - probably one of more of sort-object, where-object or foreach-object

  7. iSazonov commented on Oct 26, 2017

    @iSazonov
    Collaborator

    Is our docs well or we need add anything in the cmdlet help to address the Issue?

  8. markekraus commented on Oct 26, 2017

    @markekraus
    Contributor

    Ilya (@iSazonov) The docs could be improved with an example to calls out the common gotcha.

  9. lzybkr commented on Oct 26, 2017

    @lzybkr
    Contributor

    I can see adding a rule to ScriptAnalyzer, but I'm not sure the behavior should be changed.

    I will say most places I see this pattern, it is not necessary.

    Consistency is important here. For example, if the properties are dynamically selected, selecting one or more properties shouldn't cause weird problems downstream.

  10. HumanEquivalentUnit commented on Oct 26, 2017

    @HumanEquivalentUnit
    ContributorAuthor

    Mark Kraus (@markekraus) "where do you see that?" - imagine you want to write lines of text to a plain text file, and the accepted way was to use Export-Csv but tag on the parameter -ExpandProperty to tell it not to write a CSV. Is that something you'd expect to be within Export-Csv's remit? Would you be looking there for that feature, or think that was an odd place to put it? With that comparison, would you say Select-Object with a parameter is the best place to select non-PSObject output?

  11. HumanEquivalentUnit commented on Oct 26, 2017

    @HumanEquivalentUnit
    ContributorAuthor

    I'll note that the question in the title was really "Can that experience be improved for beginners?" instead of just "change select-object yes/no". The consideration of dynamic (wildcard) property selection is another convincing dealbreaker against that.

    Jason Shirk (@lzybkr) "I will say most places I see this pattern, it is not necessary." - most places you see ExpandProperty it is not necessary, or most places you see selecting one property is not necessary?

    As another idea for an approach, what if the default output formatter included a line of type information, or other type marker in the output? So when you do Get-ChildItem c:\path | select-object name and see a screen full of names, and the header scrolled off, there's still some indication that you have not-text. If the type matters and you need to know it - can it be made visible?

    StackOverflow has ~400 comments relating to '%expandproperty%' and picking some to see either code suggestions to use ExpandProperty, or people giving thanks for suggesting it, e.g.

    PostId	Text
    42003324	Thanks, included -expandproperty
    36187162	`|Select -ExpandProperty HomeDrivePath`
    44873829	`Select-Object -ExpandProperty UserName`
    36658641	I always forget about `-ExpandProperty`!
    45428798	`| select -ExpandProperty SAMAccountName |`
    17870097	Thank-you the, -ExpandProperty Node, did it :)
    42003324	or select -expandproperty to just get the value
    29017197	Thank you for that. `-ExpandProperty`...who knew.
    33171965	@DaveSexton `Select  -ExpandProperty Measurements`
    39231089	Try `select-object -ExpandProperty DisplayVersion`
    38185398	Get-AzureRmLocation | Select -ExpandProperty Location
    27942669	Nice. Didn't know about expandProperty. Thanks Duncan.
    33017054	Should not it be `Select-Object -ExpandProperty Index`?
    33507224	I think it should be select count -ExpandProperty Count.
    40345578	`Select-Object mail` ? `Select-Object -ExpandProperty mail`
    6375323	You're just emulating a `select -expandproperty size` there.
    38338320	try changing that last part to `select -expandproperty ipaddress`
    23139592	See my edit and tell me if -ExpandProperty shows the entire value.
    25261455	cheers, both options worked. I'll go and read up on -expandproperty
    21394652	Alternatively you could do `Select-Object -ExpandProperty identity`.
    14139818	Thanks @Kayasax, was spot on !! ( I didn't knew about ExpandProperty)
    30718643	Thanks!  `Select-Object -ExpandProperty FullName`  is working for me.
    39268354	Use `Select-Object -ExpandProperty` instead of `Select-Object -Property`
    18145840	I used -ExpandProperty name to get exactly value named "Administratoren".
    29973404	Change `select SAMAccountName` to `select -ExpandProperty SAMAccountName`
    43764032	What if you change Select-Object Name to Select-Object -ExpandProperty Name
    25395736	Also: `measure-command { ... } | select-object -expandproperty TotalSeconds`
    29067832	Have you tried `Select -ExpandProperty Servername` instead of `Format-Table`?
    40331593	Hi, try `$listoflogs = Get-EventLog -List | Select-Object -ExpandProperty Log`.
    25566906	Do it work with Select-Object -ExpandProperty instead of Select-Object -Property?
    33021473	Ah I thought its good for storing as well. Thank you!  -ExpandProperty works good.
    17581252	Thanks @jbsmith! The `-ExpandProperty` parameter to `Select-Object` did the trick.
    [etc.]
    

    and imagining that's the forever-future as one by one, people fall into that trap and have to be helped past it.

    Help.
    Use -expandproperty
    Thanks.

    Help.
    Use -expandproperty
    Thanks.

    Help.
    Use -expandproperty
    Thanks.

    Help.
    Use -expandproperty
    Thanks.

    In a world where that is not happening anymore, and Select-Object is not broken, what else could have changed to smooth over that annoyance?

  12. markekraus commented on Oct 26, 2017

    @markekraus
    Contributor

    What else could have changed to smooth over that annoyance?

    Forced education camps, maybe? /s

    I mean.. this is more about a fundamental difference between PowerShell and other languages and just about every novice (even those experienced in other languages, maybe even especially so) is going to run into this. It's like return and "leaky" functions. These are core paradigm differences about the language that can only be learned about either by actually studying the language before coding it, or through trial and error.

    Documentation may help, but only if users read it. A warning would be annoying for a non-novice who knows what they're doing. PSScriptAnalyzer rule would be nice.... if a novice knew about those, they probably already know better.

  13. lzybkr commented on Oct 26, 2017

    @lzybkr
    Contributor

    HumanEquivalentUnit - most uses of Select-Object actually - with or without -ExpandProperty, with one or more property names. But my experience could be highly skewed, so fee free to ignore if you think I'm way off. ;)

  14. iSazonov commented on Oct 27, 2017

    @iSazonov
    Collaborator

    :-) Looking on StackOverflow has ~400 comments "I didn't knew about ExpandProperty" I think we must install PowerShell in "Beginner Mode" with only available Get-Help cmdlet and unlock rest cmdlets only after the beginner pass an exam for 1000 points.

  15. markekraus commented on Oct 27, 2017

    @markekraus
    Contributor

    Ilya (@iSazonov) Like a tutorial level in games.

    Though, on a more serious note... Maybe if pwsh had a -Tutorial switch with a guided REPL that would maybe help.

  16. 13 remaining items

  17. markekraus commented on Nov 4, 2017

    @markekraus
    Contributor

    If there were a hypothetical interactive tutorial for the local console, wouldn't it be better for someone to make a PowerShell Module as opposed to a modification/add to PowerShell itself as a feature?

    That is where I would start, but, there is some merit in it being a native part of the system. The problem is that someone new to the language who just runs pwsh in their console for the first time probably has no clue what modules are or how to install them (and even as a veteran user, I have to say that PowerShellGallery is not the most user friendly tool once you know about it). They have several hurdles of knowledge and acclamation to jump first. If the underlying goal of HumanEquivalentUnit's request was to make this easier for the novice user, then including an interactive tutorial with PowerShell and not apart from it makes perfect sense.

    The part about the tutorial being forced was only a jest. Ilya (@iSazonov) recently had #5275 merged which adds a small banner on how to get help. That banner could just be expanded slightly to include something like or 'Start-Tuorial' for an interactive tutorial. The interactive tutorial could very well be a separate module and only loaded when called. It could even be just a stub that begins by walking the user through installing the actual tutorial module from the the gallery, that way the initial footprint could be small.

    In any case, this is all just brain storming.

    Should this issue be closed?

    I have changed the label to be a discussion. I'll leave this open for a few more days to see if there is any more feed back and then close it. The consensus appears to be that select-object shouldn't be changed, but the discussion as started to hit at solutions for the root cause.

  18. ScriptAutomate commented on Nov 21, 2017

    @ScriptAutomate
    Contributor

    Mark Kraus (@markekraus) HumanEquivalentUnit Okay, cool. That makes sense, and you're both right that the guidance for a newbie would be great. Would have been helpful for most others when we were starting out.

    I noticed the new small banner for help by that Ilya (@iSazonov) merged, which looks great. Reminds me of ipython's initial banner:

    Python 3.6.2 (default, Sep 10 2017, 19:48:49) 
    Type 'copyright', 'credits' or 'license' for more information
    IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: 
    

    I like the idea of it being a stub for installing the tutorial, perhaps walking the user through basic commands and diving into them as objects via the pipeline. The idea sort of reminds me of the Start-Demo module (or the modified version StartDemo), if perhaps there was a default demo maybe. I haven't tested that module out in a good while -- and I'm not sure if it needs any modification for Core, but it may be a starting point.

  19. SteveL-MSFT commented on Nov 22, 2017

    @SteveL-MSFT
    Member

    I like the idea of Start-Tutorial (perhaps a community driven module...), although not keen on having it in the core package. Would prefer we add the feature #5523 so perhaps the first time they run pwsh, we can suggest they try Start-Tutorial if this is their first time.

  20. BrucePay commented on Mar 9, 2018

    @BrucePay
    Collaborator

    We should probably note somewhere (select-object docs?) that if you just want to get the value of a single property, you should do
    ls | foreach name
    instead of
    ls | select name

  21. SteveL-MSFT commented on Mar 12, 2018

    @SteveL-MSFT
    Member

    Bruce Payette (@BrucePay) you should probably submit a PR to fix Select-Object doc which is where people might see this

  22. Pxtl commented on Oct 21, 2021

    @Pxtl

    A programming language should make the easy things simple. In this problem, the most common use-case has been made complicated, creating a jagged edge for developers - one that experienced developers are familiar with working around, but trips up newbies. In C#, the default use of "select" is to work like "expandproperty" and the Powershell approach of creating a hashtable is "select new {}", and this is much more sensible.

    "foreach" works, as Bruce Payette (@BrucePay) mentioned, but it's quite counterintuitive to use "foreach" that way.

    Basically, this is a language-design failure that is unfortunately going to live forever because of backwards compatibility. The only real way out would be to introduce a new commandlet or alias with an intuitive name.

  23. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  24. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  25. added
    Resolution-No Activity
    on Nov 16, 2023
  26. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  27. Pxtl commented on Nov 16, 2023

    @Pxtl

    Could we get new built-in commandlets to make this easy?

    Select-PropertyExpanded which would work the same as Select-Object -ExpandProperty except the default parameter would be -ExpandProperty and the Property member wouldn't exist?

    With a nice alias of expandprop?

    So we can just say ls | expandprop Name

  28. microsoft-github-policy-service commented on Nov 24, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  29. Website-monitoringdour commented on Oct 17, 2025

    @Website-monitoringdour
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-DiscussionResolution-No Activity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions