Select-Object cannot access built-in HashTable Properties

In PowerShell 7 (Preview 5), Select-Object cannot access built-in hashtable members and throws an exception instead.

In PowerShell 7 (Preview 5), Select-Object cannot access built-in hashtable members and throws an exception instead.

With hashtables, there are two sets of properties: the built-in properties like Keys and Count, and the dynamically added properties (the actual keys). Select-Object apparently cannot access the built-in properties.

Expected Behavior

In the example below, it correctly retrieves the content of property Version:

$host

The result:

Name             : ConsoleHost
Version          : 7.0.0-preview.5
InstanceId       : bdc67c11-8fe0-40e8-90fa-46326c9d0095
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
$host | Select-Object -ExpandProperty Version

The result:

Major  Minor  Build  Revision
-----  -----  -----  --------
7      0      0      -1

Problem with Hashtable Members

When you create a hashtable, it has a built-in property called Keys.

$PSVersionTable is a built-in variable which contains a hashtable that can serve as an example:

$PSVersionTable

The result:

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.5
PSEdition                      Core
GitCommitId                    7.0.0-preview.5
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
$PSVersionTable.Keys

The result:

PSVersion
PSEdition
GitCommitId
OS
Platform
PSCompatibleVersions
PSRemotingProtocolVersion
SerializationVersion
WSManStackVersion

When you look at the content of Keys, it correctly lists the keys of the hashtable.

Built-In Members are Affected

When you try and do the same with Select-Object, it cannot find the property:

$PSVersionTable | Select-Object -ExpandProperty Keys

The result:

Select-Object: Property "Keys" cannot be found.

The same applies for other built-in hashtable members such as Count. The behavior in Windows PowerShell is:

$PSVersionTable | Select-Object -ExpandProperty Keys

The result:

PSVersion
PSEdition
PSCompatibleVersions
BuildVersion
CLRVersion
WSManStackVersion
PSRemotingProtocolVersion
SerializationVersion

The same is true for the parameter -Property:

$PSVersionTable | Select-Object -Property Keys, Count

The result:

Keys Count
---- -----

Again, the built-in properties are not accessible. The behavior on Windows PowerShell is:

$PSVersionTable | Select-Object -Property Keys, Count

The result:

Keys                                                          Count
----                                                          -----
{PSVersion, PSEdition, PSCompatibleVersions, BuildVersion...}     8

Hashtable Keys are not Affected

Select-Object works as expected with hashtable keys:

# create a hashtable:
$hash = @{}

# built-in members are not found:
$hash | Select-Object -ExpandProperty Keys

The result:

Select-Object: Property "Keys" cannot be found.
# add keys:
$hash.Name = "Tobias"

# hashtable keys are found:
$hash | Select-Object -ExpandProperty Keys
$hash | Select-Object -ExpandProperty Name

The result:

Tobias

Conclusion

Apparently, there is a bug in the code that accesses hashtable properties: the code does not appropriately combine the properties from the hashtable keys with the built-in properties.

The code solely looks at the keys and misses (shadows in some sense) the built-in members.

Workaround

Until this is fixed, a workaround for -ExpandProperty is to use Foreach-Object instead:

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

The result:

PSVersion
PSEdition
GitCommitId
OS
Platform
PSCompatibleVersions
PSRemotingProtocolVersion
SerializationVersion
WSManStackVersion

There is no workaround for the parameter -Property.

Issue

This was opened as issue. See there for details on progress.