I want to delete a specific value from hashtable.
The table looks like this:
Name Value
---- -----
column {test, test2}
How can I delete the "test2" value?
I tried the following:
$myhashtable.remove("test2")
which unfortunately does not work.
Could someone help me with this? Thanks!
Hash table in the PowerShell session is created temporarily. It is like a variable, when the session is closed, hash table is deleted automatically. If you want to delete all the values from the hash table at once but retaining the hash table variable, you need to use the Clear() method.
To display a hash table that is saved in a variable, type the variable name. By default, a hash tables is displayed as a table with one column for keys and one for values. Hash tables have Keys and Values properties. Use dot notation to display all of the keys or all of the values.
@{} in PowerShell defines a hashtable, a data structure for mapping unique keys to values (in other languages this data structure is called "dictionary" or "associative array"). @{} on its own defines an empty hashtable, that can then be filled with values, e.g. like this: $h = @{} $h['a'] = 'foo' $h['b'] = 'bar'
To create a hash table in PowerShell, you'll use an @ symbol followed by an opening curly brace and a closing curly brace as shown below. Here you can see my hash table is now three lines with a key/value pair in the middle.
The example that you provided looks like a hashtable of hashtables. So you would need to do:
$myhashtable['column'].Remove('test2')
If it is a hashtable where the value is an array, then you would need to do this:
$myHashTable['column'] = ($myHashTable['column'] | ?{$_ -ne 'test2'})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With