PowerShell Hashtable with Example

In this article, we learn what is PowerShell hashtable and how it works, and some important methods that can be used with a hashtable.

 

PowerShell Hashtable

 

What is PowerShell Hashtable?

A hashtable in PowerShell is a data collection that stores key-value pairs. It is similar to a dictionary in other programming languages. You can use the “@{}” notation to create a hashtable.

Here is the syntax of the PowerShell hashtable where we have key/value pair.

$hashtableName = @{
"Key1" = "Value1"
"Key2" = "Value2"
"Key3" = "Value3"

}

 

Example –


$employees = @{
"Tom" = @{
 "EmpID" = 1
 "Age" = 30
 "Department" = "IT"
 "Salary" = 60000
}

"Barb" = @{
 "EmpID" = 2
 "Age" = 25
 "Department" = "HR"
 "Salary" = 50000
}

"Mike" = @{
 "EmpID" = 3
 "Age" = 35
 "Department" = "Finance"
 "Salary" = 65000
}
}

 

In this example, we created a hashtable called $employees, and added three key-value pairs to it, where the key is the employee’s name and the value is a nested hashtable containing information about the employee’s employee Id, age, department, and salary.

 

PowerShell hashtable Add

You can use Add() method to add new key-value pairs to the hashtable after it has been created, like below –


$hashtableName.Add("Key4", "Value4")

 

And if you want to add a new employee in the above hashtable use the Add() like below –


$employees.Add("Jen", @{

"Age" = 40
"Department" = "Admin"
"Salary" = 70000

})

 

PowerShell hashtable Remove key

To remove the key from the hashtable use the Remove() method-

Example –


$ hashtableName.Remove("Key2")

 

And to remove the key from an employees hashtable use the key name.

Example-


$employees.Remove("Mike")

PowerShell hashtable get value by key

Use the square bracket notation to access the value of a key in a hashtable like below –

Example –


$value = $employees["Mike"]

 

The value of the key “Mike” in the $employees hashtable will be stored in the $value variable. You can use the dot notation to access the value of a key in a nested hashtable, like below –

 

Example –


$age = $employees["John"].Age
Write-Host $age

 

It returns the age value from the “John” key Likewise, you can get the other properties from the hashtable. There are some other methods as well that can be used to get value by key from hashtables to see the below examples –

the Get_Item() method is used to access the value of a key in a hashtable, like below –


$value = $employees.Get_Item("Mike")
Write-Host $value

 

Also, the Item() property can be used to access the value of a key in a hashtable, like below –

$value = $employees.Item("Mike")
Write-Host $value

 

It’s important to understand that when you try to access a key that does not exist in the hashtable, it will return $null. So, it’s a good practice to validate first if a key exists in the hashtable before trying to access its value.

 

PowerShell hashtable foreach loop

 

To iterate the PowerShell hashtable using foreach loop use the “GetEnumerator()” method to enumerate all the key-value pairs in the hashtable, like below –


foreach ($employee in $employees.GetEnumerator()) {

Write-Host "Name: $($employee.Key)"
Write-Host "Employee ID: $($employee.Value.EmpID)"
Write-Host "Age: $($employee.Value.Age)"
Write-Host "Department: $($employee.Value.Department)"
Write-Host "Salary: $($employee.Value.Salary)"

}

Conclusion

I hope now you understand how to play with hashtables and how you can use Add, remove Iterate hashtable key/values items.