Converting the Dictionary Object's Key Property

Definition: Change the value of the given key.


You cannot directly change the key of a key/item pair in a hash table. To do the equivalent in PowerShell you need to add the new key/item pair and then delete the old pair. In this example, we’re changing the key carrot to celery. We do that by adding a new key/item pair to the hash table. The new key is celery, and the new item is the item from the carrot key/item pair. We then delete the carrot key/item pair:

$food.Add("celery", $food.Item("carrot"));

First we called the Add method to add the new key/item pair. The first parameter we pass to Add is the new key value. The second parameter is interesting. In the second parameter, we’ve retrieved the Item value of the key/item pair with a key of carrot. We now have a new key/item pair, with a key of celery, and a value of vegetable (which is the value of the carrot/vegetable key/item pair). Next we simply delete the carrot key/item pair by calling the Remove method.

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