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
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
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
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.
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.
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
Reacted by Keith Hill, Mark Kraus, Troy Campbell, lukeb1961, Derek Ardolf, Brian Scholer, Rob, Chad McCaffery and Melissa Leslie
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?
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
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.
Reacted by Mark Kraus, Brian Scholer, Michael Klement and Rob
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?
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?
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.
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. ;)
:-) 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.
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.
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.
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.
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
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.
microsoft-github-policy-service commented on Nov 16, 2023
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.
microsoft-github-policy-service commented on Nov 16, 2023
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.
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.
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
microsoft-github-policy-service commented on Nov 24, 2023
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
Activity
iSazonov commentedon Oct 26, 2017
I wonder why a beginner don't read a cmdlet description and examples.
markekraus commentedon Oct 26, 2017
I think changing
select-objectwould 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 withselect-objectfirst because i is a common cmdletd.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
ValueFromPipelineByPropertyNamebut 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)Definitely not for the reason mentioned above.
I don't think this one is a good idea either.
That would get confusing with a difference in operation between
Select-Object Porp1andSelect-Object Prop1, Prop2.I don't think it's worth adding a completely other cmdlet for this.
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-Csvcmdlet has an example of avoiding the mangling of objects caused by passing objects toFormat-Tablefirst. i suspect any "fix" done inSelect-Objectwould just push that initial exposure to objects on to another cmdlet.RichardSiddaway commentedon Oct 26, 2017
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
HumanEquivalentUnit commentedon Oct 26, 2017
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?
RichardSiddaway commentedon Oct 26, 2017
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
iSazonov commentedon Oct 26, 2017
Is our docs well or we need add anything in the cmdlet help to address the Issue?
markekraus commentedon Oct 26, 2017
Ilya (@iSazonov) The docs could be improved with an example to calls out the common gotcha.
lzybkr commentedon Oct 26, 2017
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.
HumanEquivalentUnit commentedon Oct 26, 2017
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-Csvbut tag on the parameter-ExpandPropertyto tell it not to write a CSV. Is that something you'd expect to be withinExport-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 saySelect-Objectwith a parameter is the best place to select non-PSObject output?HumanEquivalentUnit commentedon Oct 26, 2017
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 nameand 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.
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
-expandpropertyThanks.
Help.
Use
-expandpropertyThanks.
Help.
Use
-expandpropertyThanks.
Help.
Use
-expandpropertyThanks.
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?
markekraus commentedon Oct 26, 2017
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
returnand "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.
lzybkr commentedon Oct 26, 2017
HumanEquivalentUnit - most uses of
Select-Objectactually - 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. ;)iSazonov commentedon Oct 27, 2017
:-) 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.
markekraus commentedon Oct 27, 2017
Ilya (@iSazonov) Like a tutorial level in games.
Though, on a more serious note... Maybe if
pwshhad a-Tutorialswitch with a guided REPL that would maybe help.13 remaining items
markekraus commentedon Nov 4, 2017
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
pwshin 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.
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-objectshouldn't be changed, but the discussion as started to hit at solutions for the root cause.ScriptAutomate commentedon Nov 21, 2017
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:
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-Demomodule (or the modified versionStartDemo), 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.SteveL-MSFT commentedon Nov 22, 2017
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 runpwsh, we can suggest they tryStart-Tutorialif this is their first time.BrucePay commentedon Mar 9, 2018
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 nameinstead of
ls | select nameSteveL-MSFT commentedon Mar 12, 2018
Bruce Payette (@BrucePay) you should probably submit a PR to fix
Select-Objectdoc which is where people might see thisPxtl commentedon Oct 21, 2021
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
Pxtl commentedon Nov 16, 2023
Could we get new built-in commandlets to make this easy?
Select-PropertyExpandedwhich would work the same asSelect-Object -ExpandPropertyexcept the default parameter would be-ExpandPropertyand thePropertymember wouldn't exist?With a nice alias of
expandprop?So we can just say
ls | expandprop Namemicrosoft-github-policy-service commentedon Nov 24, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.
Website-monitoringdour commentedon Oct 17, 2025
https://practicablemsgpack.github.io/9m39w4DtJH5CzIYIoSNdwhCIMAspqCqpEEot1Q3yf33atHG6uIXwFRLS/