Table of Contents
What is an array?
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 C:\> $arr = 10,20,30,40,50
PS C:\> $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 5, use the following command:
PS C:\> $col = 1..5
PS C:\> $col
1
2
3
4
5
When no data type is specified, Windows PowerShell creates each array as an object array 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 C:\> $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 C:\> [int32[]]$iarr = 50,100,150,200,250
PS C:\> $iarr
50
100
150
200
250
As a result, the $iarr array can contain integer values only.
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 C:\> $arr = @()
PS C:\> $arr.Count
0
PS C:\> $arr = @('10','20','Chris','@')
PS C:\> $arr.count
4
PS C:\> $arr
10
20
Chris
@
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 C:\> $obj = @(Get-Process powershell)
PS C:\> $obj
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
681 33 76612 88380 0.08 35820 1 powershell
691 33 76920 88496 0.03 38248 1 powershell
821 32 93312 108712 0.23 42264 1 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 C:\> $arr = 10,20,30,40,50
PS C:\> $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:
#Display the first element in the $arr array
PS C:\> $arr[0]
10
#Display the second element in the $arr array
PS C:\> $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 C:\> $arr
10
20
30
40
50
PS C:\> $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 C:\> $arr.Length
5
PS C:\> $arr.count
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 C:\> 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 C:\> $arr
10
20
30
40
50
PS C:\> $arr[1] = 1000
PS C:\> $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 C:\> $arr.SetValue(500,1)
PS C:\> $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 C:\> $arr += 60
PS C:\> $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 C:\> $newarr = $arr[0,1 + 3..($arr.length - 1)]
PS C:\> $newarr
10
500
40
50
60
PS C:\> $arr.count
6
PS C:\> $newarr.count
5
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 C:\> $a = 5,10
PS C:\> $b = 15,20
PS C:\> $c = $a + $b
PS C:\> $c
5
10
15
20
PS C:\> $d = $b + $a
PS C:\> $d
15
20
5
10
To delete an array, assign a value of $null to the array. The following command deletes the array in the $c variable.
PS C:\> $c = $null
PS C:\> $c
PS C:\>
You can also use the Remove-Item cmdlet but assigning a value of $null is faster, especially for large arrays.