Powershell: Read-Host result doesn't work with hashtable lookup

Created on 11 Sep 2016  路  2Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

PS C:\dev\PowerShell> $response = Read-Host 'foo'
foo: 2
PS C:\dev\PowerShell> $response
2
PS C:\dev\PowerShell> $response -eq 2
True
PS C:\dev\PowerShell> $h = @{2 = @('A')}
PS C:\dev\PowerShell> $h[$response]    # BUG!!
PS C:\dev\PowerShell> $h[2]
A
PS C:\dev\PowerShell>

Expected behavior

$h[$response] should be A

Actual behavior

$h[$response] is $null

Environment data

Windows inbox PS

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.0.10586.494
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.10586.494
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

OS X

> $PSVersionTable
Name                           Value                                                                                                                                  
----                           -----                                                                                                                                  
PSVersion                      6.0.0-alpha                                                                                                                            
PSEdition                      Core                                                                                                                                   
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                
BuildVersion                   3.0.0.0                                                                                                                                
GitCommitId                    v6.0.0-alpha.9                                                                                                                         
CLRVersion                                                                                                                                                            
WSManStackVersion              3.0                                                                                                                                    
PSRemotingProtocolVersion      2.3                                                                                                                                    
SerializationVersion           1.1.0.1                                                                                                                                
Issue-Bug Resolution-By Design WG-Engine

All 2 comments

I believe that this is because the 2 is being seen type converted to an int as opposed to a string in

$h = @{2 = @('A')}

Whereas if you where to do the below and replace the 2 with a k (an example)

$response = Read-Host 'foo' 
$response
$response -eq 'k'
$h = @{k = @('A')}
$h[$response]
$h['k']

this can be confirmed as well with the below

PS C:\> $h = @{'2' = @('A')}
PS C:\> $h.Keys
2
PS C:\> $h.ContainsKey(2)
False
PS C:\> $h.ContainsKey('2')
True
PS C:\> $h = @{2 = @('A')}
PS C:\> $h.Keys
2
PS C:\> $h.ContainsKey(2)
True
PS C:\> $h.ContainsKey('2')
False

Ah, good point @kilasuit !

Was this page helpful?
0 / 5 - 0 ratings