Converting the Dictionary Object's Item Property

Definition: Sets or retrieves an item given a key.


You retrieve the value of the item for the key by calling the Item method, passing the method the name of the key. In this example we retrieve the item associated with the key carrot:

$type = $food.Item("carrot")

Item is really shorthand for Get_Item. And what’s the opposite of Get_Item? That’s right, Set_Item. We set the item in a key/item pair by calling Set_Item, passing it the name of the key associated with the item we want to change, and the new value of the item. Here we’re changing the value of the item for the key carrot:

$food.Set_Item("carrot", "fruit")

Another way of doing this is to use the Item method to retrieve the hash object, then assign a value to it:

$food.Item("carrot") = "fruit"

See conversions of other Dictionary object methods and properties.
Return to the VBScript to Windows PowerShell home page