TOPIC
about_arrays
SHORT DESCRIPTION
Describes a compact data structure for storing data elements.
LONG DESCRIPTION
An array is a data structure for storing a collection of data elements
of the same type. Windows PowerShell supports data elements, such as
string, int (32-bit integer), long (64-bit integer), bool (Boolean), byte,
and other Microsoft .NET Framework object types.
CREATING AND INITIALIZING AN 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 $A that contains the seven
numeric (int) values of 22, 5, 10, 8, 12, 9, and 80, type:
$A = 22,5,10,8,12,9,80
You can also create and initialize an array by using the range
operator (..). For example, to create and initialize an array named
“$B” that contains the values 5 through 8, type:
$B = 5..8
As a result, $B contains four values: 5, 6, 7, and 8.
When no data type is specified, Windows PowerShell creates each array as
an object array (type: object[]). To determine the data type of an array,
use the GetType() method. For example, to determine the data type of the
$a array, type:
$a.gettype()
To create a strongly typed array, that is, an array that can contain only
values of a particular type, cast the Variable as an array type, such
as string[], long[], 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 $ia containing four integers (1500, 2230, 3350,
and 4000), type:
[int32[]]$ia = 1500,2230,3350,4000
As a result, the $ia array can contain only integers.
You can create arrays that are cast to any supported type in the
Microsoft .NET Framework. For example, the objects that Get-Process
retrieves to represent processes are of the System.Diagnostics.Process
type. To create a strongly typed array of process objects, enter the
following command:
[Diagnostics.Process[]]$zz = Get-Process
You can populate an array by using the output of a cmdlet, Function, or
statement. For example, the following statement creates an array that
contains the processes that start with the letters “co” and that are
running on the current computer:
$LocalProcesses = Get-Process co*
If the statement gets only a single process, the $LocalProcesses Variable
would not be an array. To ensure the command creates an array, use the
array subexpression operator, @, as shown in the following example:
$LocalProcesses = @(Get-Process co*)
Even if the command returns a single process, the $LocalProcesses Variable
is an array. Even if it has only one member, you can treat it like any
other array. For example, you can add other objects to it. For more
information, see about_operators.
READING AN ARRAY
You can refer to an array by using its Variable name, such as $A or $a.
Windows PowerShell is not case-sensitive.
To display all the elements in the array, type the array name. For example:
$a
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 $a array, type:
$a[0]
To display the third element in the $a array, type:
$a[2]
Negative numbers count from the end of the array. For example, “-1”
refers to the last element of the array. To display the last three elements
of the array, type:
$a[-3..-1]
However, be cautious when using this notation.
$a[0..-2]
This command does not refer to all the elements of the array, except for
the last one. It refers to the first, last, and second-to-last elements
in the array.
You can use the range operator to display a subset of all the values in an
array. For example, to display the data elements at index position 1
through 3, type:
$a[1..3]
You can use the plus operator (+) to combine a range with a list of
elements in an array. For example, to display the elements at index
positions 0, 2, and 4 through 6, type:
$a[0,2+4..6]
To determine how many items are in an array, combine the range with the
length property of an array. For example, to display the elements from
index position 2 to the end of the array, type:
$a[2..($a.length-1)]
The length is set to -1 because the index begins at position 0. Therefore,
in a three-element array (1,2,3), the index of the third element is 2,
which is one less than the length of the array.
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 $a array, type:
foreach ($element in $a) {$element}
The Foreach loop iterates through the array and returns each value in
the array until reaching the end of the array.
The For loop is useful when you are incrementing counters while examining
the elements in an array. For example, to return every other value in an
array by using a For loop, type:
for ($i = 0; $i -le ($a.length – 1); $i += 2) {$a[$i]}
You can use a While loop to display the elements in an array until a
defined condition is no longer true. For example, to display the elements
in the $a array while the array index is less than 4, type:
$i=0
while($i -lt 4) {$a[$i]; $i++}
To learn about the properties and methods of an array, such as the Length
property and the SetValue method, use the InputObject parameter of the
Get-Member cmdlet. When you pipe an array to Get-Member, it displays
information about the objects in the array. When you use the InputObject
parameter, it displays information about the array.
To find the properties and methods of the $a array, type:
Get-Member -inputobject $a
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 $a array (index
position 1) to 10, type:
$a[1] = 10
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 $a
array to 500:
$a.SetValue(500,1)
You can append an element to an existing array by using the += operator.
This operator adds to an existing value. When the operator is used on an
element of an array, it increases the value of the element. When the
operator is used on the array itself, it appends the value. For example,
to append an element with a value of 200 to the $a array, type:
$a += 200
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, to create the $t array with all the elements in the $a array
except for the value at index position 2, type:
$t = $a[0,1 + 3..($a.length – 1)]
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.
$x = 1,3
$y = 5,9
$z = $x + $y
As a result, the $z array contains 1, 3, 5, and 9.
To delete an array, use the Remove-Item cmdlet to delete the Variable that
contains the array. The following command specifies the element “a” in the
Variable: drive.
(For more information about the Variable: drive, see the Variable provider
Help topic.)
SEE ALSO
about_Assignment_Operators
about_hash_tables
about_operators
about_For
about_Foreach
about_While