Book Image

Mastering Windows PowerShell Scripting

By : Brenton J.W. Blawat
Book Image

Mastering Windows PowerShell Scripting

By: Brenton J.W. Blawat

Overview of this book

Table of Contents (22 chapters)
Mastering Windows PowerShell Scripting
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Arrays


Arrays are the second most used containers in PowerShell. An array, in simple terms, is a multi-dimensional variable or a variable containing more than one value. The two core components to an array are the index number and the position value. When you use an array, you reference an index number and it will return the position value.

Single-dimension arrays

The following table represents an array with a single dimension:

Index number

Position value

0

Example 1

1

Example 2

2

Example 3

3

Example 4

4

Example 5

When you are storing, manipulating, or reading the data in an array, you have to reference the position in the array the data is residing. The numbers populated in the table's Index number column are representative of the location within the array. You will see that array's numbering starts at the number 0, and so the first data would be in cell 0. If you call the array at position 0, the result would be the position value of Example 1. When building the array, you will see that each value in the array values is separated by a comma. This tells the PowerShell interpreter to set a new array value.

First, you can start by building the array in the preceding table by entering the following command:

$myArray = "Example 1", "Example 2", "Example 3", "Example 4", "Example 5"
$myArray

The output of this is shown in the following screenshot:

The preceding example displays how to create an array of strings. You first start by declaring a variable named $myArray. You then place multiple strings of text separated by commas to build the array. After declaring the array, you call the $myArray array to print the values to the console. It will return Example 1, Example 2, Example 3, Example 4, and Example 5.

Retrieving data at a specific position in an array is done through the use of brackets. To retrieve the value of 0 from the array, you would do the following:

$myArray = "Example 1", "Example 2", "Example 3", "Example 4", "Example 5"
$myArray[0]

The output of this is shown in the following screenshot:

The preceding example displays how you can obtain array data at a specific position. You first start by declaring a variable named $myArray. You then place multiple strings of text separated by commas to build the array. After declaring the array, you call $myArray[0] to access the position value of index number 0 from the array. The preceding example returns the value of Example 1 for the index number 0.

Jagged arrays

Arrays can become more complex as you start adding dimensions. The following table represents a jagged array or an array of arrays:

Index number

Position value 0

Position value 1

0

Example 1

Red

1

Example 2

Orange

2

Example 3

Yellow

3

Example 4

Green

4

Example 5

Blue

While accessing data in a jagged array, you will need to read the cell values counting at 0 for both dimensions. When you are accessing the data, you start reading from the index number first and then the position value. For example, the Example 1 data is in the index number of 0 and the position value of 0. This would be referenced as position [0][0]. Subsequently, the data Blue is in the index number of 4 and position value of 1. This would be referenced as position [4][1].

To do this for yourself, you can build the preceding table by entering the following command:

$myArray = ("Example 1","Red"), ("Example 2","Orange"), ("Example 3", "Yellow"), ("Example 4", "Green"), ("Example 5", "Blue")
$myArray[0][0]
$myArray[4][1]

The output is shown in the following screenshot:

This example displays how to create a jagged array and accessing values in the array. You first start building the jagged array by declaring the first array of "Example 1" "Red", second array of "Example 2" "Orange", third array of "Example 3" "Yellow", fourth array of "Example 4" "Green", and fifth array of "Example 5" "Blue". After building the array, you access the word Example 1 by referencing $myArray[0][0]. You then access the word Blue by referencing $myArray[4][1].

Updating array values

After you create an array, you may need to update the values inside the array itself. The process for updating values in an array is similar to retrieving data from the array. First you need to find the cell location that you want to update, and then you need to set that array location as equal to the new value:

Index number

Position value 0

Position value 1

0

John

Doe

1

Jane

Smith

Given the preceding table, if Jane's last name needed to be updated to display Doe instead of Smith, you would first need to locate that data record. That incorrect last name is located at index number 1 and position value 1, or [1][1]. You will then need to set that data location equal (=) to Doe.

To do this, you need to enter the following command:

$myArray = ("John","Doe"), ("Jane","Smith")
$myArray
$myArray[1][1] = "Doe"
$myArray

The output of this is shown in the following screenshot:

This example displays how you can create an array and update a value in the array. You first start by defining $myArray and use "John","Doe", "Jane", and "Smith" as the array values. After calling the variable to print the array to the screen, you update the value in index number 1, position value 1, or $myArray[1][1]. By setting this position equal to Doe, you change the value from Smith to Doe:

Index number

Position value 0

Position value 1

0

John

Doe

1

Jane

Smith

2

Sam

Smith

In instances where you want to append additional values to the array, you can call the array variable with the += command and the data you want to add to the array. This looks like $array += "New Array Values". The += command is a more efficient method of performing the commands $array = $array + "New Array Values".

To add data into an array and make the preceding table, you can do the following operation:

# Create the Array
$myArray = ("John","Doe"), ("Jane","Smith")
$myArray
# Append Data to the Array
$myArray += ("Sam","Smith")
$myArray

The output of this is shown in the following screenshot:

In this example, you add values to an existing array. You first start by defining an array of $myArray. You then print the existing contents of the array to the screen. You then add additional content by setting the array += to the new array data of ("Sam","Smith"). After reprinting the contents of the array to the screen, you see the values Sam and Smith added to the array.

Tip

To search and remove items from an array, you will need to create a ForEach loop to cycle through all of the index numbers and position values. Chapter 4, Functions, Switches, and Loop Structures, explores the ForEach looping structure.