Add variable value to HashTable as key

Powershell V5 Windows 10

For example, a variable $EmployeeID

contains a string testValue

, I just want to use the value of the previous variable inside the function $HastTable.Add()

. It will look like this:

$HashTable.Add($EmployeeID, 'some_value')

      

Other than that it won't work, but hopefully I understand what I want to achieve.

With this, I can access the value as usual:

$var = $HashTable.testValue

      

Simple question and I'm sure I'm missing something obvious, but I've tried searching the web and I couldn't find anything.

+3


source to share


1 answer


You are doing it right and should be able to access the key without any problem (you don't get auto completion this way):

$HashTable = @{ }
$EmployeeID = "testValue"
$HashTable.Add($EmployeeID, 'some_value')
$var = $HashTable.testValue
$var

      



Output:

some_value

      

+3


source







All Articles