Kae Travis

Using a Hashtable to store Key/Value Pairs

This is a simple example of using a Hashtable to store key/value pairs. Rather than referencing a value in an array using a numeric index, we now have named (the key) human-readable values that we can use to reference a stored value inside the Hashtable:

#create hashtable
$states = @{}
#add a key/value pair
$states.Add("ExampleKey", "ExampleValue1")
#two ways of getting the value
write-host $states.Get_Item("ExampleKey")
write-host $states.ExampleKey
#update the value for the key 'ExampleKey'
$states.Set_Item("ExampleKey", "ExampleValue2")
#two ways of getting the value
write-host $states.Get_Item("ExampleKey")
write-host $states.ExampleKey

 

Using a Hashtable to store Key/Value Pairs
Using a Hashtable to store Key/Value Pairs

Leave a Reply