Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete value from hashtable in powershell

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!

like image 738
Adeel ASIF Avatar asked May 06 '13 14:05

Adeel ASIF


People also ask

How do you clear a Hashtable in PowerShell?

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.

How do you access a Hashtable in PowerShell?

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.

What is @{} in PowerShell?

@{} 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'

How does PowerShell define hash table?

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.


1 Answers

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'})
like image 154
EBGreen Avatar answered Sep 30 '22 14:09

EBGreen