I\'m trying to loop through a hash table and set the value of each key to 5 and PowerShell gives an error:
$myHash = @{}
$myHash[\"a\"] = 1
$myHash[\"b\"] =
Use clone
:
foreach($key in ($myHash.clone()).keys){ $myHash[$key] = 5 }
Or in the one-liner:
$myHash = ($myHash.clone()).keys | % {} {$myHash[$_] = 5} {$myHash}
There is a much simpler way of achieving this. You cannot change the value of a hashtable while enumerating it because of the fact that it's a reference type variable. It's exactly the same story in .NET.
Use the following syntax to get around it. We are converting the keys collection into a basic array using the @()
notation. We make a copy of the keys collection, and reference that array instead which means we can now edit the hashtable.
$myHash = @{}
$myHash["a"] = 1
$myHash["b"] = 2
$myHash["c"] = 3
foreach($key in @($myHash.keys)){
$myHash[$key] = 5
}
You do not need to clone the whole hashtable for this example. Just enumerating the key collection by forcing it to an array @(...)
is enough:
foreach($key in @($myHash.keys)) {...
I'm new to PowerShell, but I'm quite a fan of using in-built functions, because I find it more readable. This is how I would tackle the problem, using GetEnumerator and Clone. This approach also allows one to reference to the existing hash values ($_.value) for modifying purposes.
$myHash = @{}
$myHash["a"] = 1
$myHash["b"] = 2
$myHash["c"] = 3
$myHash.Clone().GetEnumerator() | foreach-object {$myHash.Set_Item($_.key, 5)}