How To Use Hash Tables With PowerShell

In this blog post, I will show you how to use Hash Tables with PowerShell and explain why they are so useful.

About Hash Tables

Microsoft PowerShell Hash Tables are dictionaries or arrays that allow us to store data in a structured way using key pair values.

Create an Hash Table

Let’s go ahead and create a simple Hash Table that stores a hostname and an IP address, as shown below.

$computers = @{  localhost = "127.0.0.1"; windows10 = "10.0.1.4"}

To access the Hash Table and use it with a PowerShell cmdlet, I can use the Test-Connection cmdlet as shown below.

test-connection -TargetName $computers.windows10au

To view the content of an Hash Table you just type the name of the Hash Table as a variable.

$computers

To view the values or keys use the following options.

$computers .values
$computer.keys

The above example is very simple however in cases where there is a need to use key values pairs Hash Tables are very useful compared to using variables.

Processing…
Success! You're on the list.


Posted

in

by