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
:
- .keys
- Description: Retrieves all the keys from the hashtable.
- Output:
'x','y'
- This returns the keys
x
andy
as a collection.
- .values
- Description: Retrieves all the values from the hashtable.
- Output:
'10','20'
- This returns the values
10
and20
corresponding to the keysx
andy
.
- .y
- Description: Accesses the value associated with the key
y
. - Output:
20
- This directly retrieves the value
20
linked to the keyy
.
- Description: Accesses the value associated with the key
- .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.
- .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.
- .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.
- Description: Iterates over the hashtable and formats each key-value pair as
- [‘y’]
- Description: Accesses the value associated with the key
y
. - Output:
20
- Similar to the direct access with
.y
, this returns the value20
linked toy
.
- Description: Accesses the value associated with the key
- .Remove(‘x’)
- Description: Removes the key-value pair associated with the key
x
. - Output:
@{y=20}
- After removing the key
x
, only the keyy
remains with its value20
.
- Description: Removes the key-value pair associated with the key
- .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 returnsTrue
.
- Description: Checks if the key
- .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 value30
is added to the hashtable.
- Description: Adds a new key-value pair where
- .Clear()
- Description: Clears all key-value pairs from the hashtable.
- Output:
@{}
- This removes all entries from the hashtable, leaving it empty.
- $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.