Associative Arrays in PowerShell

In this PowerShell tutorial, I will explain everything about Associative arrays in PowerShell. Like, I will explain what associative arrays are and how to use associative arrays in PowerShell with examples.

What is an Associative Array in PowerShell?

In PowerShell, an associative array is a data structure that stores pairs of keys and values. Unlike a regular array that uses numeric indexes, an associative array uses unique keys to access stored values. This makes it an ideal structure for storing related information where each element can be quickly retrieved without knowing its position in the collection.

Create Associative Arrays in PowerShell

To create an associative array in PowerShell, you use the @{} syntax. Here’s a simple example:

$person = @{
    Name = "John Doe"
    Age = 30
    JobTitle = "Software Developer"
}

In this example, Name, Age, and JobTitle are the keys, and each key is associated with a value. You can think of it as a small database where you can look up Age by referencing John Doe.

Adding and Modifying Items from Associative Array

You can add or modify items in an associative array by directly referencing the key in PowerShell:

$person = @{
    Name = "John Doe"
    Age = 30
    JobTitle = "Software Developer"
}

# Adding a new key-value pair
$person['Department'] = 'Engineering'

# Modifying an existing key-value pair
$person['Age'] = 31

You can see the output in the screenshot below:

Associative Arrays in PowerShell

Retrieving Values from Associative Arrays

To retrieve a value, you reference the associative array with the key like below:

# Retrieving the value of 'Name'
$personName = $person['Name']

Removing Items from Associative Array

To remove an item from an associative array in PowerShell, use the Remove method:

# Removing the 'JobTitle' key-value pair
$person.Remove('JobTitle')

Iterating Over Associative Arrays

You can iterate over an associative array using a foreach loop:

foreach ($key in $person.Keys) {
    $value = $person[$key]
    Write-Host "$key: $value"
}

Checking for Keys

To check if a key exists in the hash table, use the -contains keyword:

if ($person.ContainsKey('Age')) {
    Write-Host "Age is in the hash table."
}

Working with Complex Structures

Associative arrays can also store arrays or even other associative arrays as values:

$company = @{
    Name = 'TechCorp'
    Employees = @(
        @{
            Name = 'John Doe'
            Position = 'Developer'
        },
        @{
            Name = 'Jane Smith'
            Position = 'Designer'
        }
    )
}

Cloning Hash Tables

To create a copy of a hash table, use the Clone method:

$personCopy = $person.Clone()

Splatting with Associative Arrays

Splatting is a feature in PowerShell that allows you to pass a collection of parameter values to a command using an associative array:

$params = @{
    Path = 'C:\Temp'
    ItemType = 'Directory'
}

New-Item @params

Converting to and from JSON

You can convert associative arrays to JSON strings and vice versa:

# Convert to JSON
$jsonString = $person | ConvertTo-Json

# Convert from JSON
$personFromJson = $jsonString | ConvertFrom-Json

Conclusion

In this PowerShell tutorial, I have explained how to work with associative arrays in PowerShell. Also, I have explained how to create, modify, and use associative arrays in PowerShell.

  • What is an Associative Array in PowerShell?
  • Create Associative Arrays in PowerShell
  • Adding and Modifying Items from Associative Array
  • Retrieving Values from Associative Arrays
  • Removing Items from Associative Array
  • Iterating Over Associative Arrays
  • Checking for Keys
  • Working with Complex Structures
  • Cloning Hash Tables
  • Splatting with Associative Arrays
  • Converting to and from JSON

You may also like: