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!
The comments above are both correct answers. I'm adding them as answers to close the question.
By PetSerAl:
$choices.[int]$selection
Or by beatcracker:
$choices = @{ '0' = 'SelectionA'}
This is one of the PowerShell's automatic type conversion blindspots. Keys in your hashtable are integers, but Read-Host returns strings.