PowerShell Array

Like any other programming language, the array is a type of data structure that can be used to store a collection of items of similar type but unlike any strongly typed language such as C, C# or java in PowerShell it’s not mandatory to specify the data type of array, the collection of items can be either of the same datatype or different.

The elements of array saved in memory and can be accessed using the index. The index of the array starts at zero (0), so to access the first element you must use the index [0]. Typically, there are various operations can be performed on array-like adding an element to the array or removing an element, sort elements, change elements, etc.

In this article, we are going to learn about PowerShell array in detail.

Advantages of using arrays

  1. Array store element in memory and it allow random access of elements. This makes accessing elements by position faster.
  2. Arrays have better cache locality so that retrieval of array elements is faster.
  3. Arrays are reusable, once it is declared they can be reused multiple times.

How to declare a PowerShell Array

There are many ways to declare an array either you can declare it using datatypes or without datatypes in PowerShell.

Examples

1.Declare using a comma-separated list

PS C:\> $Color = ‘Red’,’Yellow’,’Green’

Here the array is declared using name Color and the “Red” element is on position 0, yellow at 1, and Green at position 2.

2.Declare using @ ()

The same array can be declared using @ () like below

PS C:\> $Color = @(‘Red’,’Yellow’,’Green’)

3. Strongly Typed array

Where datatypes are explicitly mentioned at the time of declaration –

PS C:\>[string[]] $Color = ‘Red’,’Yellow’,’Green’

These are the popular ways of declaring an array in PowerShell the internal functionality is the same but little difference at the time of declaration.

Read an Array in PowerShell

If you the index of element simply access the array using it’s index if you don’t know the index then traverse the elements of an array using the loop.

PS C:\> $Color = ‘Red’,’Yellow’,’Green’

In above example $color[0] will return Red and likewise $color[1], $color[2] will return yellow and green.

PowerShell Array

How to check the length of an array

Array length is the count of elements in that array. It helps when you need to check the array is empty or it has elements. Use “.length” property to get the array length or count.

Below is an example of how to get a length of an array.

PS C:\> $Color = ‘Red’,’Yellow’,’Green’
PS C:\> $Color.length

get powershell array length

 

 

 

How to add an element in PowerShell array

Adding elements to an array. Let us see an example of how to add an element to an existing array of string.

Example –

The ‘+=’ operator is used to add element is array.it always adds at last in an array.

PS C:\> $color += “Blue”

add item in array

 

 

 

 

One item “Blue” has been added to the array and count increase to one now total array count is four.

How to update an element in an array

Array index can be used to update existing elements in an array. See below example –

PS C:\> $color = ‘Red’,’Yellow’,’Green’,’Blue’
PS C:\> $color[3] = “LightBlue

update powershell array

 

Here the array element which exists on index[3] has been updated to “LightBlue”.

Finding an element in an array

Use “-like” operator to find an element in an array.

Example –

PS C:\> $color = ‘Red’,’Yellow’,’Green’,'LightBlue','Pink', 'LightYellow'
PS C:\> $color -like '*Light*'

In this example “Light” word has been used to search elements in an array –

find element in array

Sorting element in an array

The sort operator can be used to sort the elements of an array if the elements are of the same datatype. Sorting can be ascending or descending by default it is ascending.

PS C:\> $color = ‘Red’,’Yellow’,’Green’,'LightBlue','Pink','LightYellow'
PS C:\> $color sort

sort element in powershell array

The above command sort the elements in ascending order.

Loop through an array

In order to handle each element in an array, need to use foreach loop to iterate items in an array. For example, if we declare a string array and want to print each element of the array, we should run the following script:

$Color = 'Red','Yellow','Green','LightBlue','Pink','LightYellow'
if($Color.Length -gt 0)
{
foreach($arr in $Color) {
 Write-Host "The color name is :" + $arr
 }
}
else {
 Write-Host "Array is empty"
}

loop all array elements in powershell

I hope now you have a basic understanding of PowerShell array.