Powershell: Select-Object -ExpandProperty does not work with Hashtable members

Created on 17 Nov 2019  路  10Comments  路  Source: PowerShell/PowerShell

Select-Object reports an error when -ExpandProperty is used on a hashtable member that is not a key.

Version:
PowerShell 7 Preview 5

Reproduce:
$PSVersionTable | Select-Object -expandProperty Keys

Expected Result:
[list of hashtable keys]

Actual Result:
Exception:
Select-Object: Property "count" cannot be found.

Workaround:
$PSVersionTable | ForEach-Object { $_.Keys }

Details:
https://powershell.one/bugs/powershell-7/select-object

Area-Cmdlets-Utility Issue-Bug

Most helpful comment

@TobiasPSP I think that should do it. I have a hunch it won't make it in preview6 but as a bug fix it might still scrape by and make it in for 7.0 GA ^^

All 10 comments

@TobiasPSP, was this issue created by an automated process from the linked page?

Either way, it would help if the OP (original post) used proper code formatting.

And, just to clarify (as also explained in the linked page), the issue is not specific to the -ExpandProperty parameter - it just fails more noisily in that case - Select-Object [-Property] is equally affected.

Omitting -ExpandProperty (implied or explicit -Property) simply creates a $null-valued property due to not consulting the .NET-type-native properties:

PS> $PSVersionTable | Select-Object Keys

Keys
----
                # Property value is $null

@mklement0 : the issue was not autogenerated. I was working on https://powershell.one/tricks/performance/group-object when I came across the bug and opened an issue here. After I posted the issue, later I realized that the bug affects Select-Object in general and not just -expandProperty. After updating the article, I returned here to update the issue but you already took care of it.

What do you mean by "it would help if the OP used proper code formatting"? [EDITED: meanwhile I guess I figured out what you meant: adding markdown to format the code. That too would be an excellent bullet point for detailed instructions as suggested below. It's not ignorance but rather lack of routine to know about the required markdown or the editing commands.}

To be honest, I had a hard time finding a place to submit the issue in the first place. My natural first stop was at https://github.com/PowerShell/PowerShell. In the section "Developing and Contributing", I eventually followed the link to the "Contribution Guide", then followed the steps outlined in "Contributing to Issues", and finally clicked "open a new issue".

This left me with a blank form w/o any instructions, so I did my best to describe the problem properly. I remember when I opened the issue https://github.com/PowerShell/PowerShell/issues/10982, I followed a different link with a helpful issue template but was unable to find that link again anywhere.

IMHO it would be helpful if the landing page https://github.com/PowerShell/PowerShell had a distinct section about feature requests and issue reporting, including the link to the issue template. This would help everyone and ensure reported issues are formatted correctly and contain all required information.

There is a design flaw in Group-Object. With a workaround, your scripts can be up tp 50x faster and still 2x faster on PowerShell Core.
GitHub
PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.

Omitting -ExpandProperty (implied or explicit -Property) simply creates a $null-valued property due to not consulting the .NET-type-native properties:

PS> $PSVersionTable | Select-Object Keys

Keys
----
                # Property value is $null

Agree, the NULL value is expected behavior for any property not found on the original object:

PS C:\Users\tobia> $PSVersionTable  | Select-Object -Property Test, Tobias

Test Tobias
---- ------

So the issue seems to be related specifically to the dual nature of Hashtables where properties can originate from native .NET properties exposed by the hashtable plus the dynamically added props from hashtable keys. Apparently, Select-Object priorizes the dynamically added props and "forgets" about the native hashtable props. This also explains the behavior with -ExpandProperty.

I think I can take a look at this, sounds like it should be a relatively straightforward fix to do. 馃檪

@TobiasPSP I think that should do it. I have a hunch it won't make it in preview6 but as a bug fix it might still scrape by and make it in for 7.0 GA ^^

Awesome response time, I am honestly impressed! Many thanks for the great work.

@TobiasPSP, re reporting issues:

Thanks for the pointer regarding the new-issue link in https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md - this is now fixed, and points to https://github.com/PowerShell/PowerShell/issues/new/choose, where you're offered templates for various issue types, which do have fenced code blocks for proper code rendering (```powershell\n...\n```).

I had a hard time finding a place to submit the issue in the first place. My natural first stop was at https://github.com/PowerShell/PowerShell.

Yes, and the Issues tab (<repo-url>/issues) is a standard part of every repo page on GitHub; clicking on the New Issue button there also takes you to https://github.com/PowerShell/PowerShell/issues/new/choose, the page with the issue templates.

However, I've just submitted two more PRs:

GitHub
PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.
GitHub
GitHub is where people build software. More than 40 million people use GitHub to discover, fork, and contribute to over 100 million projects.
GitHub
PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.
GitHub
PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.

That's great - fixing the links will help everyone. Thanks for looking into this so quickly!

My pleasure, @TobiasPSP. Note that I'm just a fellow contributor myself and that's it pretty easy to create PRs to propose such simple documentation changes.

:tada:This issue was addressed in #11097, which has now been successfully released as v7.1.0-preview.4.:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings