Home » General Scripting and Coding » PowerShell » Using a Hashtable for Key/Value pairs

Using a Hashtable for Key/Value pairs

Posted on by

This is a simple example of how we can use a hashtable to store and update key/value pairs:

#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

 

Comments have now been disabled. If you have a question to ask about this post please ask the community!