Often you may want to update values in a hash table in PowerShell.
You can use the following methods to do so:
Method 1: Update One Specific Value in Hash Table
$my_hash['A0004'] = 50
This particular example updates the key named A0004 to have a value of 50 in the hash table named $my_hash.
Method 2: Update Multiple Values in Hash Table
foreach($key in $($my_hash.keys)){ $my_hash[$key] = 50 }
This particular example updates each key to have a value of 50 in the hash table named $my_hash.
The following examples show how to use each method in practice with the following hash table named $employees that contains keys that represent employee ID numbers and values that represent the total sales made by each employee:
$employees = [ordered]@{ 'A0001' = 24; 'A0002' = 20; 'A0003' = 14; 'A0004' = 31; 'A0005' = 9; 'A0006' = 15; 'A0007' = 22; }
Example 1: How to Update One Specific Value in Hash Table
We can use the following syntax to update the key A0004 to have a value of 50 in the hash table named $employees:
$employees['A0004'] = 50
The following screenshot shows how to use this syntax in practice:

Notice that the key A0004 now has a value of 50, just as we specified.
All other values remained unchanged in the hash table.
Example 2: How to Update Multiple Values in Hash Table
We can use the following syntax with a foreach loop to update all keys to have a value of 50 in the hash table named $employees:
foreach($key in $($employees.keys)){ $employees[$key] = 50 }
The following screenshot shows how to use this syntax in practice:

Notice that all keys in the hash table now have a value of 50, just as we specified.
Related Tutorials
The following tutorials explain how to perform other common tasks in PowerShell:
PowerShell: How to Check if Hash Table Contains Specific Key
PowerShell: How to Iterate Over a Hash Table
PowerShell: How to Remove Elements from a Hash Table
