Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Windows PowerShell Scripting

You're reading from   Mastering Windows PowerShell Scripting Master the art of automating and managing your Windows environment using PowerShell

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher
ISBN-13 9781782173557
Length 282 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Brenton J.W. Blawat Brenton J.W. Blawat
Author Profile Icon Brenton J.W. Blawat
Brenton J.W. Blawat
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Variables, Arrays, and Hashes FREE CHAPTER 2. Data Parsing and Manipulation 3. Comparison Operators 4. Functions, Switches, and Loops Structures 5. Regular Expressions 6. Error and Exception Handling and Testing Code 7. Session-based Remote Management 8. Managing Files, Folders, and Registry Items 9. File, Folder, and Registry Attributes, ACLs, and Properties 10. Windows Management Instrumentation 11. XML Manipulation 12. Managing Microsoft Systems with PowerShell 13. Automation of the Environment 14. Script Creation Best Practices and Conclusion 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:

Single-dimension arrays

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:

Single-dimension arrays

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:

Jagged arrays

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:

Updating array values

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:

Updating array values

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.

You have been reading a chapter from
Mastering Windows PowerShell Scripting
Published in: Apr 2015
Publisher:
ISBN-13: 9781782173557
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image