Working with Arrays in Windows PowerShell 1.0

From Techotopia
Revision as of 14:44, 5 May 2016 by Neil (Talk | contribs) (Text replacement - "<table border="0" cellspacing="0" width="100%">" to "<table border="0" cellspacing="0">")

Jump to: navigation, search
PreviousTable of ContentsNext
Basic Windows PowerShell 1.0 TypesWindows PowerShell 1.0 Hashtables


Purchase and download the full PDF version of this PowerShell eBook for only $8.99


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.


Contents


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:

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

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:

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

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:

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

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:

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" )
>>

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:

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

Accessing Elements in a Windows PowerShell Array

<google>ADSDAQBOX_FLOW</google> Once values have been stored in an array it is highly likely that these values will need to be 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:

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


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

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

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

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

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:

$bookinfo[1] = "Wilson, David"

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 ([]).

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

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

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

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 third element in the $library array:

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

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:

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

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:

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

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

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

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.

<google>BUY_WPS_BOTTOM</google>