PowerShell – Hashtables


Powershell – Hashtables


”;


Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. Generally we used String or numbers as keys.

This tutorial introduces how to declare hashtable variables, create hashtables, and process hashtable using its methods.

Declaring hashtable Variables

To use an hashtable in a program, you must declare a variable to reference the hashtable. Here is the syntax for declaring an hashtable variable −

Syntax

$hash = @{ ID = 1; Shape = "Square"; Color = "Blue"}
or
$hash = @{} 

Note − Ordered dictionaries can be created using similar syntax. Ordered dictionaries maintain the order in which entries are added whereas hashtables do not.

Example

The following code snippets are examples of this syntax −

$hash = [ordered]@{ ID = 1; Shape = "Square"; Color = "Blue"}

Print the hashtable.

$hash

Output

Name                           Value    
----                           -----                                                    
ID                             1                                                        
Color                          Blue                                                     
Shape                          Square 

The hashtable values are accessed through the keys.

> $hash["ID"]
 1

Processing Hashtable

Dot notation can be used to access hashtables keys or values.

> $hash.keys
ID
Color
Shape

> $hash.values
1
Blue
Square

Example

Here is a complete example showing how to create, initialize, and process hashtable −

$hash = @{ ID = 1; Shape = "Square"; Color = "Blue"}

write-host("Print all hashtable keys")
$hash.keys

write-host("Print all hashtable values")
$hash.values

write-host("Get ID")
$hash["ID"]

write-host("Get Shape")
$hash.Number

write-host("print Size")
$hash.Count

write-host("Add key-value")
$hash["Updated"] = "Now"

write-host("Add key-value")
$hash.Add("Created","Now")

write-host("print Size")
$hash.Count

write-host("Remove key-value")
$hash.Remove("Updated")

write-host("print Size")
$hash.Count

write-host("sort by key")
$hash.GetEnumerator() | Sort-Object -Property key

This will produce the following result −

Output

Print all hashtable keys
ID
Color
Shape
Print all hashtable values
1
Blue
Square
Get ID
1
Get Shape
print Size
3
Add key-value
Add key-value
print Size
5
Remove key-value
print Size
4
sort by key

Name                           Value                                                                                                   
----                           -----                                                                                                   
Color                          Blue                                                                                                    
Created                        Now                                                                                                     
ID                             1                                                                                                       
Shape                          
Square    

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *