February 2019
Intermediate to advanced
626 pages
15h 51m
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'}PS> 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