Author Posts

July 20, 2018 at 9:19 am

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!

July 20, 2018 at 1:23 pm

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
$keyvalue

Please format your code as code. Thanks

July 20, 2018 at 1:43 pm

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. 🙂

July 20, 2018 at 1:43 pm

Hello,

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.

July 20, 2018 at 3:21 pm

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.
Best,