October 2017
Intermediate to advanced
440 pages
11h 47m
English
Unlike arrays, removing an element from a hashtable is straightforward: an element is removed using the Remove method:
$hashtable = @{ Existing = "Existing" }
$hashtable.Remove("Existing")
If the requested key does not exist, the command does nothing (and does not throw an error).
The Remove method cannot be used to modify the hashtable while looping through the keys in a hashtable using the Keys property:
PS> $hashtable = @{ Key1 = 'Value1' Key2 = 'Value2'}foreach ($key in $hashtable.Keys) { $hashtable.Remove($key)}Collection was modified; enumeration operation may not execute.At line:5 char:10+ foreach ($key in $hashtable.Keys) {+ ~~~~ + CategoryInfo : OperationStopped: (:) [], InvalidOperationException ...Read now
Unlock full access