Applies to: Exchange Server 2010 SP3, Exchange Server 2010 SP2
Topic Last Modified: 2009-02-16
An array provides a data structure that can be used to store a collection of data elements of the same type. The Exchange Management Shell supports all kinds of data elements. See the following sections for information about:
For detailed information about how to use arrays, run the following command in the Shell:
Copy Code | |
---|---|
Get-Help About_Array |
Creating Arrays
You can create and initialize arrays by assigning
multiple values to a variable. The values that are stored in the
array are delimited by using a comma and are separated from the
variable name by the = assignment operator. For example,
suppose you want to create an array that is named
$Example
that contains the following seven integer
values: 22, 5, 10, 8, 12, 9, 80
. To create the array,
enter the following command:
Copy Code | |
---|---|
$Example = 22,5,10,8,12,9,80 |
In the array, the first data element is at index
position 0
, the second is at position 1
,
and so on.
Reading Arrays
You can reference an array by its variable name, such
as $Example
. You can reference a specific value within
the array by using the index number of the position in the array
where the value is stored. For example, to reference the first data
element in the $Example
array, enter the following
command:
Copy Code | |
---|---|
Write-Host $Example[0] |
The Shell will return the value 22
because
that is stored in the first array element.
Manipulating Arrays
To change the value of a single item in an array,
specify the array name, the index you want to modify, the =
assignment operator, and the new value that you want to use instead
of the existing value. For example, to change the value of the
second item in the $Example
array, index position
1
, to 10
, enter the following
command:
Copy Code | |
---|---|
$Example[1] = 10 |
You can also use the SetValue method to change a
value. The following example changes the second value, index
position 1
, of an array named $Example
to
500
:
Copy Code | |
---|---|
$Example.SetValue(500,1) |
You can append a value to the end of an existing array.
For example, to add an additional integer, such as
200
, to the $Example
array, enter the
following command:
Copy Code | |
---|---|
$Example += 200 |
Associative Arrays
Associative arrays are the same as regular arrays. However, they enable the assignment of key-value pairs to a variable. For example, you may want to assign values to keys in an array to be called on when a command is being processed. The following example creates an associative array:
Copy Code | |
---|---|
$Example = @{blue = 1; red = 2,3} |
When you enter $Example
on the command
line, you see the following output:
Copy Code | |
---|---|
Key Value --- ----- red {2, 3} blue 1 |
You can retrieve the information that is stored in the array by calling the array as follows:
Copy Code | |
---|---|
$Example.blue |
The previous example returns a value
1
.
Because multiple values were assigned to the
red
key, those values make up a nested array. You can
reference the values in this nested array by using their index
value. You can retrieve the information that is stored in the key's
nested array by calling the associative array,
$Example
, with the red
key and the index
of the nested array location that you want to retrieve
1
, as follows:
Copy Code | |
---|---|
$Example.red[1] |
The previous example returns the value 3.
For more information about associative arrays, run the following command in the Shell:
Copy Code | |
---|---|
Get-Help About_Associative_Array |