Using arrays
An array is a variable that stores multiple items that are either of the same or different data type. An array can be created by assigning multiple values to a variable separated by a comma. The operator is used to assign values is the assignment operator (=
). The following example creates an array that contains numbers 34
, 56
, 2
, 23
, and 8
:
$Array = 34, 56, 2, 23, 8
A range operator (..
) can also be used to create and initialize an array. The following example will create an array that will store the numbers from 1 to 5:
$Number = 1..5
This will store the values 1
, 2
, 3
, 4
, and 5
into the $Number
.
If you do not specify the data type during creation, PowerShell creates an array of type:System.Object[]
. You can use the GetType()
method to display the type of array object created. For instance, the data type of the $Number array
will be listed by:
$Number.GetType().
If you need an array that can have items of a particular data type, use the strongly typed array by preceding the array variable name with the data type such as the following example where $intarray
contains 32-bit integer values:
[int32[]]$intarray = 1000, 2000, 3000, 4000
This will result in $intarray
containing only integers as we have specified that no values other than int32
data type is allowed as value in this array.
In Windows Powershell, you can create arrays consisting of any supported data type in the Microsoft .Net framework.
Now, let's take an example of an array with multiple data types:
$mixedArray = 6,"Hello",4.5,"World"
The array subexpression operator is used to create an array with zero or more objects. Use the following syntax for the operator @( ... )
:
$mixedArray = @(6,"Hello",4.5,"World")
Some other examples are as follows:
The following example creates an empty array:
$EmptyArray = @()
Create an array with a single element:
$Array = @("Hello World")
Create a multidimensional array:
$MultiDimArray = @( (4,5,6), (24,30,36) )
Retrieving values from an array
If you type the array name, it will display all the items in that array:
$Array
The array number index starts at 0, so the first element will be accessed by this:
$Array[0]
This is similar to displaying the fifth element in the $Array
use:
$Array[4]
The last element of the array will be displayed by -1
. So, the following example displays the last four items:
$Array[-4..-1]
We have used the range operator (..
) here, which can also be used to display the elements from values 1
to 5
in the next example:
$Array[1..5]
The (+
) operator combines the elements of the array. Use the following example to display the elements at index 0
, 1
, 3
, and 5
to 8
:
$Array[0,1,3+5..8]
The length property of the
Count
alias will display the number of items in an array:
$Array.Count
While, ForEach
, and For
looping statements are used to display items in an array:
Foreach ($item in $Array) {$item}
The following For
statement loops through and displays every third item in the array:
For ($n = 0; $n –le ($Array.length -1); $n+=3) {$Array[$n]}
The While
statement is used to list the items in an array until the defined condition becomes false. The following example displays the items until the index position is less than 6
:
$i=0 while($i -lt 5) {$a[$i]; $i++}
Now, let's look at manipulating items in arrays. The replacement of an item in an array can be done by using the assignment (=
) operator and the index of the present item to be replaced. The following example replaces the fourth element with the value of 23
:
$Array[3] = 23
The SetValue
method can also be used to replace the value of items in an array. The following example replaces the third value (index position 2) with a value of 34
:
$Array.SetValue(34,2)
The elements are added to the array by the use of the +=
operator. The following example adds an element of 250 to the $Array array
:
$Array +=250
The default array class in PowerShell does not provide an easy way to delete the elements of the array. Array elements can be copied over to another array and old arrays can be deleted though. In the following example, the $x
array will consist of all the items from $array
except the third and fifth items:
$x = $array[0,1,3 + 5..($array.length - 1)]
Multiple arrays can be combined in a single array by using a plus operator (+
). The following example creates three arrays $a
, $b
, and $c
and combines them to $x
, which will now have all the elements of the three arrays:
$a = 2,4,6,8,10 $b = 1,3,5,7,9 $c = 11,12,13,14,15 $x= $a+$b+$c
Arrays can be deleted by assigning the value of $null
to the array; for example, $array = $null
.
For additional help on arrays, use the following cmdlet:
PS C:\> get-help about_arrays