Publish Date: September 22, 2015

Arrays in Windows PowerShell

An array is a data structure that is designed to store a collection of items. The items can be the same type or different types.  Arrays allow the user to store information in an index.

Create and Initialize Array

To create and initialize an array, assign multiple values to a variable. The values stored in the array are delimited with a comma and separated from the variable name by the assignment operator (=).

For example, to create an array named $arr that contains the five numeric (int) values 10, 20, 30, 40, and 50, use the following command:

PS D:\MyScripts> $arr = 10,20,30,40,50
PS D:\MyScripts>
PS D:\MyScripts> $arr
10
20
30
40
50

You can also create and initialize an array by using the range operator (..). For example, to create and initialize an array named $col that contains the values 1 through 10, use the following command:

PS D:\MyScripts> $col = 1..10
PS D:\MyScripts>
PS D:\MyScripts> $col
1
2
3
4
5
6
7
8
9
10

When no data type is specified, Windows PowerShell creates each array as an object array (type: System.Object[]). To determine the data type of an array, use the GetType() method. For example, to determine the data type of the $arr array, use the following command:

PS D:\MyScripts> $arr.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

To create a strongly typed array, which means an array that can contain only values of a particular data type, cast the variable as an array type, such as string[], or int32[]. To cast an array, precede the variable name with an array type enclosed in brackets. For example, to create a 32-bit integer array named $iarr containing five integers (50, 100, 150, 200, and 250), use the following command:

PS D:\MyScripts> [int32[]]$iarr = 50,100,150,200,250

As a result, the $iarr array can contain integer values only.

Array Sub-Expression Operator

You can use Array Sub-Expression Operator to create an array even if it only contains zero or one object. The syntax of the array operator is @(…). The following command creates $arr array with zero objects.

PS D:\MyScripts> $arr = @()
PS D:\MyScripts>
PS D:\MyScripts> $arr.count
0

The array operator is particularly useful in scripts when you will get the objects, but you are not sure how many objects you will get.

PS D:\MyScripts> $obj = @(Get-Process powershell)

Read an Array

You can refer to an array by using its variable name. To display all the elements in the array, type the array name.

PS D:\MyScripts> $arr
10
20
30
40
50

You can refer to the elements in an array by using an index, beginning at position 0. Enclose the index number in brackets. For example, to display the first element in the $arr array, type the following command:

PS D:\MyScripts> $arr[0]
10

To display the second element in the $arr array, type $arr[1].

PS D:\MyScripts> $arr[1]
20

The negative numbers count from the end of the array. For example, “-1” refers to the last element of the array. To display the last two elements of the array, use the following command:

PS D:\MyScripts> $arr[-2..-1]
40
50

To determine how many items are in an array, use the Length property (or count alias) as shown below.

PS D:\MyScripts> $arr.Length
5

You can also use looping constructs, such as ForEach, For, and While loops, to refer to the elements in an array. For example, to use a ForEach loop to display the elements in the $arr array, use the following command:

PS D:\MyScripts> foreach ($a in $arr)
>> {
>> $a
>> }
>>
10
20
30
40
50

For more information on looping, visit PowerShell Loops section.

Manipulating an Array

You can change the elements in an array, add an element to an array, and combine the values from two arrays into a third array.

To change the value of a particular element in an array, specify the array name and the index of the element that you want to change, and then use the assignment operator (=) to specify a new value for the element. For example, to change the value of the second item in the $arr array (index position 1) to 1000, use the following command:

PS D:\MyScripts> $arr[1] = 1000
PS D:\MyScripts> $arr
10
1000
30
40
50

You can also use the SetValue method of an array to change a value. The following example changes the second value (index position 1) of the $arr array to 500:

PS D:\MyScripts> $arr.SetValue(500,1)
PS D:\MyScripts> $arr
10
500
30
40
50

You can use the += operator to add an element to an array. When you use it, Windows PowerShell actually creates a new array with the values of the original array and the added value. For example, to add an element with a value of 60 to the array in the $arr variable, use the following command:

PS D:\MyScripts> $arr += 60
PS D:\MyScripts> $arr
10
500
30
40
50
60

It is not easy to delete elements from an array, but you can create a new array that contains only selected elements of an existing array. For example, the following command creates the $newarr array with all the elements in the $arr array except for the value at index position 2:

PS D:\MyScripts>  $newarr = $arr[0,1 + 3..($arr.length - 1)]
PS D:\MyScripts>
PS D:\MyScripts>
PS D:\MyScripts> $newarr
10
500
40
50
60

To combine two arrays into a single array, use the plus operator (+). The following example creates two arrays, combines them, and then displays the resulting combined array.

PS D:\MyScripts> $a = 5,10
PS D:\MyScripts> $b = 15,20
PS D:\MyScripts> $c = $a + $b
PS D:\MyScripts>
PS D:\MyScripts> $c
5
10
15
20

As a result the $c array contains 5, 10, 15, and 20.

To delete an array, assign a value of $null to the array. The following command deletes the array in the $c variable.

PS D:\MyScripts> $c = $null

You can also use the Remove-Item cmdlet but assigning a value of $null is faster, especially for large arrays.

Back

 



Microsoft Certified | Cisco Certified