Can a hash table be inside a variable?

I have a simple hash table application. My dilemma is that the key-value pairs are all inside a CSV file.
Will Powershell accept something like:

$x = @{$HashTab}

where $HashTab (if displayed to the console) has the contents:

‘123’=‘Smith, James’;
‘301’=‘Jones, Debbie’;

‘910’=‘Anders, Roy’;

After this, I then want to do something like a table lookup, i.e.,
$key = ‘301’
$Keyvalue = $x.$key

Any hints, tips or better ways of doing it “the Powershell way” would be highly appreciated.
Much thanks!

If $HashTab already is a hash table what for do you need to assign it to another variable?

$HashTab = @{
‘123’=‘Smith, James’
‘301’=‘Jones, Debbie’
‘910’=‘Anders, Roy’

$key = ‘301’
$keyvalue = $HashTab.$key

Please format your code as code. Thanks

If the above is what your string data looks like, then you might want to look into using the ConvertFrom-StringData cmdlet to get it into hashtable form. :slight_smile:


Thanks for your reply. The Contents of my hash table are sitting in an external file.
Short of typing the contents (rather long, about 500 entries) of that file into my PS script, I would like to know if those contents can be read into a variable as described in my initial post.

Many thanks Mr Sallow. I have just tried it and it works beautifully. The documentation suggests the command can do a lot more, I’m happy to have come across this piece of know-how.

My sincerest gratitude for your help.