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>
$h[$response] should be A
$h[$response] is $null
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
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 !