Changes

Jump to: navigation, search

Working with Arrays in Windows PowerShell 1.0

6,929 bytes added, 13:55, 19 November 2008
New page: In the previous chapter we looked at the basic Windows PowerShell data types. This chapter will begin to look at some more complex data types, starting with ''arrays'' and ''multidimension...
In the previous chapter we looked at the basic Windows PowerShell data types. This chapter will begin to look at some more complex data types, starting with ''arrays'' and ''multidimensional arrays''. An array allows a collection of values to be stored and accessed via a single variable. Unlike some languages, Windows PowerShell does not restrict the elements of an array to being of the same type (a concept referred to as ''polymorphic''). For example, it is perfectly valid for some array elements to be Int32 values whilst others are Strings. Each item is accessed in the array variable through the use of an ''index'' value into the array.

== Creating a Windows PowerShell Array ==

A Windows PowerShell array is actually an instance of the .NET System.object[] type and may created simply by assigning a comma separated list of values to a variable:

<pre>
PS C:\Users\Administrator> $bookinfo = 857643, "Smith, John", "The Widget Reference Guide"
</pre>

The above command creates an array named $bookinfo and assigns three elements to the array consisting of a number and two strings. Another way to create an array is to explicitly specify the type at creation time:

<pre>
PS C:\Users\Administrator> [Array] $bookinfo = 857643, "Smith, John", "The Widget Reference Guide"
</pre>

When creating an array with a single element, the array type must be explicitly defined, or the sole element pre-fixed with a comma. The reason for this is that without some indication that an array is being created, PowerShell will see the single element value and assume the variable to be a basic type equivalent to the value being assigned:

<pre>
[Array] $bookinfo = 857643 # Creates an array with a single element
$bookinfo = ,857643 # Also creates an array with a single element
</pre>

== Creating Windows PowerShell Multidimensional Arrays ==

A multidimensional Windows PowerShell array is nothing more than an array in which each array element is itself an array. A multidimensional array can, therefore, be thought of as a table, where each element in the parent array represents a row of the table and the elements of each child array represent the columns of the row.

Multidimensional arrays are created in PowerShell by encapsulating each array element in parentheses and separating the elements with commas. The following provides an example of a multidimensional array where a ''$library'' array is assigned an array for each element containing information for each book:

<pre>
PS C:\Users\Administrator> $library = ( 857643, "Smith, John", "The Widget Reference Guide" ),
>> ( 857644, "William, Paul", "The Life of a Widget" ),
>> ( 857645, "Peters, Karl", "A Widget of Honor" )
>>
</pre>

== Obtaining the Length of an Array ==

The length of a Windows PowerShell array may be obtained by accessing the array's ''Length'' property using dot notation. For example, to get the length of the ''$bookinfo'' array:

<pre>
PS C:\Users\Administrator> $bookinfo.length
3
</pre>

== Accessing Elements in a Windows PowerShell Array ==

Once values have been stored in an array it is highly likely that these values will need to accessed at some later point. This is achieved using the array accessor notation combined with the index into the array of the desired value. The array accessor is simply the array name followed by square brackets. Within the square brackets is placed a number representing the index into the array of the desired element (keeping in mind that the first array element in PowerShell is index 0, not 1). For example, to access the second element of our ''$bookinfo'' array, the following notation would be used:

<pre>
PS C:\Users\Administrator> $bookinfo[1]
Smith, John
</pre>


A range of array elements may be accessed by specifying the start and end element within the square brackets, separated by two dots:

<pre>
PS C:\Users\Administrator> $bookinfo[0..1]
857643
Smith, John
</pre>

In addition, multiple elements may accessed by specifying a comma separated list of index values. For example, the following extracts the first and third elements from the ''$bookinfo'' array:

<pre>
PS C:\Users\Administrator> $bookinfo[0,2]
857643
The Widget Reference Guide
</pre>

This approach may similarly be used to change the value of an array element. Once again, this involves the use of the index into the array, but this time combined with the '=' assignment operator to assign a new value. The following command changes the name of the author in the ''$bookinfo'' array:

<pre>

</pre>

== Accessing Elements in a Windows PowerShell Multidimensional Array ==

To understand accessing elements of a multidimensional array it helps to continue to use the table analogy we started in an earlier section. Assuming a multidimensional array, we need to first specify the array row that we wish to access. Secondly, we need to specify the column in that row. To access an element, therefore, we specify the array name and then follow it with the desired row and column of the array, each enclosed in square brackets ([]).

<pre>
PS C:\Users\Administrator> $bookinfo[1] = "Roberts, Arthur"
PS C:\Users\Administrator> $bookinfo[1]
Roberts, Arthur
</pre>

For example, suppose we wish to access the author of the second book in our ''$library'' array:

<pre>
PS C:\Users\Administrator> $library[1][1]
William, Paul
</pre>

In addition, it is also possible to access the entire array element of a multidimensional array simply by using the index of that element. In the following example, the entire book array is displayed for the thrid element in the ''$library'' array:

<pre>
PS C:\Users\Administrator> $library[2]
857645
Peters, Karl
A Widget of Honor
</pre>

== Adding new Elements to a Windows PowerShell Array ==

New elements may be added to an array using the += operator. In the following example, a new element containing publisher information is added to our ''$bookinfo'' array:

<pre>
PS C:\Users\Administrator> $bookinfo += "Pulp Publishers"
PS C:\Users\Administrator> $bookinfo
857643
Roberts, Arthur
The Widget Reference Guide
Pulp Publishers
</pre>

== Combining Windows PowerShell Arrays ==

Windows PowerShell arrays may be combined using the + and += operators. The += operator is used to append one array to the end of another array without the need to create a new array:

<pre>
PS C:\Users\Administrator> $myarray1 = 1,2,3,4
PS C:\Users\Administrator> $myarray2 = 5,6,7,8
PS C:\Users\Administrator> $myarray1 += $myarray2
PS C:\Users\Administrator> $myarray1
1
2
3
4
5
6
7
8
</pre>

When combining two arrays together to created a new array, the + operator is used:

<pre>
PS C:\Users\Administrator> $myarray3 = $myarray1 + $myarray2
PS C:\Users\Administrator> $myarray3
1
2
3
4
5
6
7
8
</pre>

In the above example, $myarray1 and $myarray2 remain unchanged, but a new array, named $myarray3, has been created which contains the combined elements of $myarray1 and myarray2.

Navigation menu