Professional Documents
Culture Documents
About Arrays - Help
About Arrays - Help
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.
$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
$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
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
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
$a[0]
$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]
$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)]
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:
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:
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.
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.
$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)
$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:
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
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.
remove-item variable:a
(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