PowerShell Ordered Arrays [With Examples]

In this PowerShell tutorial, I will explain what is an ordered array in PowerShell with examples and how to work with PowerShell ordered arrays.

An ordered array in PowerShell, often referred to as an ordered hashtable, is a special kind of hashtable that maintains the order of its key-value pairs as they were added. It is created using [ordered] before the hashtable syntax, for instance $ordered = [ordered]@{Key1=’Value1′; Key2=’Value2′}, ensuring that elements are retrieved in the same order they were defined.

What is an Ordered Array in PowerShell?

An ordered array, also known as an ordered dictionary, is a data structure in PowerShell that stores pairs of keys and values in a specific sequence. Unlike a regular array, which is indexed by numbers, an ordered array is indexed by keys, which allows you to maintain the order in which items are added. This is particularly useful when the sequence of elements is important and must be preserved.

How to Create an Ordered Array in PowerShell?

To create an ordered array in PowerShell, you use the [ordered] attribute before a hashtable. Here’s a simple example:

$orderedArray = [ordered]@{
    "First" = "1st Place"
    "Second" = "2nd Place"
    "Third" = "3rd Place"
}

In this example, $orderedArray is an ordered array that will maintain the order of the keys “First”, “Second”, and “Third” as they were added.

Add Elements to an Ordered Array

You can add elements to an ordered array in PowerShell by using the .Add() method. Here’s how you can add a new key-value pair to the existing ordered array:

$orderedArray.Add("Fourth", "4th Place")

After this operation, “Fourth” will be the last key in the $orderedArray.

Access Elements in an Ordered Array

To access the elements in an ordered array, you can use the key associated with the value you want to retrieve. For example:

$orderedArray = [ordered]@{
    "First" = "1st Place"
    "Second" = "2nd Place"
    "Third" = "3rd Place"
}
$secondPlace = $orderedArray["Second"]
Write-Output $secondPlace

This will output 2nd Place.

After executing the script using VS code, you can see the output in the screenshot below:

PowerShell Ordered Arrays

Modify Elements in an Ordered Array

If you need to change a value associated with a key in the ordered array, you can do so by directly referencing the key:

$orderedArray = [ordered]@{
    "First" = "1st Place"
    "Second" = "2nd Place"
    "Third" = "3rd Place"
}
$orderedArray["Second"] = "Runner-up"

Now, the value for the key “Second” has been changed to Runner-up.

Remove Elements from a PowerShell Ordered Array

To remove elements from an ordered array, you can use the .Remove() method with the key of the element you want to remove:

$orderedArray = [ordered]@{
    "First" = "1st Place"
    "Second" = "2nd Place"
    "Third" = "3rd Place"
}
$orderedArray.Remove("Third")

This operation will remove the key-value pair with the key “Third” from the $orderedArray.

Iterate Through an Ordered Array

To iterate through each key-value pair in the ordered array in PowerShell, you can use a foreach loop:

$orderedArray = [ordered]@{
    "First" = "1st Place"
    "Second" = "2nd Place"
    "Third" = "3rd Place"
}
foreach ($key in $orderedArray.Keys) {
    Write-Output "$key: $($orderedArray[$key])"
}

This will output each key followed by its associated value.

Sorting an Ordered Array

By default, an ordered array maintains the order of elements as they are added. However, if you need to sort the array, you would have to convert it to a regular array or a different data structure that supports sorting. For example, you could use the Sort-Object cmdlet to sort the array by value or key:

$orderedArray = [ordered]@{
    "First" = "1st Place"
    "Second" = "2nd Place"
    "Third" = "3rd Place"
}
$sortedArray = $orderedArray.GetEnumerator() | Sort-Object Value

This will create a new array that is sorted by the values of the original ordered array.

Conclusion

Ordered arrays in PowerShell allow you to maintain a sequence of key-value pairs. They are particularly useful when the order of elements is crucial. Using the methods shown above, you can easily create, modify, access, and iterate through ordered arrays.

I will explain how to work with the PowerShell ordered arrays in this PowerShell tutorial. With examples, we checked:

  • What is an Ordered Array in PowerShell?
  • How do you create an ordered array in PowerShell?
  • Add Elements to an Ordered Array
  • Access Elements in an Ordered Array
  • Modify Elements in an Ordered Array
  • Remove Elements from a PowerShell Ordered Array
  • Iterate Through an Ordered Array
  • Sorting an Ordered Array

You may also like: