Search code examples
powershellhashmap

Powershell hashtable in quotes


In Powershell, I have a hashtable called $derivedkey with 2 key-value pairs.

I want to use the values in my hash table inside a double quote like this:

write-host "mysalt = $derivedkey['salt']"

But what I get back is:

mysalt = System.Collections.Hashtable['salt']

Is there a way such that I can get powershell to evaluate the hashmap to the value, so it will return this?

mysalt = F668844CD184B7549E00DCBE51274730

Solution

  • You can wrap it in a sub-expression like this:

    write-host "mysalt = $($derivedkey['salt'])"
    

    Otherwise it stops as soon as the end of a valid variable name is reached, and ['salt'] is not a part of the variable name, so it's trying to interpret the entire hashtable not just the 'salt' entry.