This image provides an overview of various PowerShell Hashtable methods. A Hashtable in PowerShell is a data structure that stores key-value pairs. The table shows different commands that can be used to manipulate or retrieve data from a hashtable, along with their expected outputs.

Here’s a breakdown of the methods and their functions, based on the provided input @{x=10; y=20}, which is a simple hashtable with keys x and y, and their respective values 10 and 20:

  1. .keys
    • Description: Retrieves all the keys from the hashtable.
    • Output: 'x','y'
    • This returns the keys x and y as a collection.
  2. .values
    • Description: Retrieves all the values from the hashtable.
    • Output: '10','20'
    • This returns the values 10 and 20 corresponding to the keys x and y.
  3. .y
    • Description: Accesses the value associated with the key y.
    • Output: 20
    • This directly retrieves the value 20 linked to the key y.
  4. .GetEnumerator() | ForEach {$_.key}
    • Description: Iterates over the hashtable and returns the keys.
    • Output: 'x','y'
    • This method uses an enumerator to loop through the hashtable and list all the keys.
  5. .GetEnumerator() | ForEach {$_.value}
    • Description: Iterates over the hashtable and returns the values.
    • Output: '10','20'
    • This method uses an enumerator to loop through the hashtable and list all the values.
  6. .GetEnumerator() | ForEach {$.key + ‘=’ + $.value}
    • Description: Iterates over the hashtable and formats each key-value pair as key=value.
    • Output: 'x=10','y=20'
    • This creates a string that displays each key and its corresponding value.
  7. [‘y’]
    • Description: Accesses the value associated with the key y.
    • Output: 20
    • Similar to the direct access with .y, this returns the value 20 linked to y.
  8. .Remove(‘x’)
    • Description: Removes the key-value pair associated with the key x.
    • Output: @{y=20}
    • After removing the key x, only the key y remains with its value 20.
  9. .containskey(‘x’)
    • Description: Checks if the key x exists in the hashtable.
    • Output: 'True'
    • This checks if the key x is present in the hashtable and returns True.
  10. .z=30
    • Description: Adds a new key-value pair where z=30.
    • Output: @{x=10; y=20; z=30}
    • A new key z with the value 30 is added to the hashtable.
  11. .Clear()
    • Description: Clears all key-value pairs from the hashtable.
    • Output: @{}
    • This removes all entries from the hashtable, leaving it empty.
  12. $newHT = $ht.Clone()
    • Description: Creates a copy of the hashtable.
    • Output: @{x=10; y=20}
    • This makes a new hashtable ($newHT) that is an identical copy of the original hashtable.

This table offers a helpful reference for manipulating hashtables in PowerShell, providing several ways to access, modify, and query data within a hashtable structure. Each method has its specific use, allowing developers to efficiently handle collections of key-value pairs.

Credit @dfinke for the image.