I am trying to let a user make a selection and then return a value from a hashtable based on that selection.
My hashtable looks like this:
$choices = @{ 0 = "SelectionA";
1 = "SelectionB";
99 = "SelectionC"}
My selection looks like this:
$selection = Read-Host -Prompt "
Please make a selection
0 - Selection A
1 - Selection B
99 - Selection C "
Then I'm trying to bring back the value based on the selection like this:
$choices.$selection
or
$choices.{$selection}
This isn't working. Is it possible to call a hashtable value using a variable as the key?
Thanks for any help you can offer!
$choices.[int]$selection
– Trevelyan$choices = @{ '0' = 'SelectionA'}
:). This is one of the PowerShell's automatic type conversion blindspots. Keys in your hashtable are integers, butRead-Host
returns strings. – Disk