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
Microsoft Exchange Server PowerShell Essentials

You're reading from   Microsoft Exchange Server PowerShell Essentials Leverage the power of basic Windows PowerShell scripts to manage your Exchange messaging environment

Arrow left icon
Product type Paperback
Published in Feb 2016
Publisher
ISBN-13 9781782176039
Length 210 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Biswanath Banerjee Biswanath Banerjee
Author Profile Icon Biswanath Banerjee
Biswanath Banerjee
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Getting Started with PowerShell 2. Learning Recipient Management FREE CHAPTER 3. Handling Distribution Groups 4. Exchange Security 5. Everything about Microsoft Exchange Policies 6. Handling Exchange Server Roles 7. Auditing and E-Discovery 8. Managing High Availability 9. Exploring EWS Managed API 10. Common Administration Tasks Index

Using arrays

An array is a variable that stores multiple items that are either of the same or different data type. An array can be created by assigning multiple values to a variable separated by a comma. The operator is used to assign values is the assignment operator (=). The following example creates an array that contains numbers 34, 56, 2, 23, and 8:

$Array = 34, 56, 2, 23, 8

A range operator (..) can also be used to create and initialize an array. The following example will create an array that will store the numbers from 1 to 5:

$Number = 1..5

This will store the values 1, 2, 3, 4, and 5 into the $Number.

If you do not specify the data type during creation, PowerShell creates an array of type:System.Object[]. You can use the GetType() method to display the type of array object created. For instance, the data type of the $Number array will be listed by:

$Number.GetType().

If you need an array that can have items of a particular data type, use the strongly typed array by preceding the array variable name with the data type such as the following example where $intarray contains 32-bit integer values:

[int32[]]$intarray = 1000, 2000, 3000, 4000

This will result in $intarray containing only integers as we have specified that no values other than int32 data type is allowed as value in this array.

In Windows Powershell, you can create arrays consisting of any supported data type in the Microsoft .Net framework.

Now, let's take an example of an array with multiple data types:

$mixedArray = 6,"Hello",4.5,"World"

The array subexpression operator is used to create an array with zero or more objects. Use the following syntax for the operator @( ... ):

$mixedArray = @(6,"Hello",4.5,"World")

Some other examples are as follows:

The following example creates an empty array:

$EmptyArray = @()

Create an array with a single element:

$Array = @("Hello World")

Create a multidimensional array:

$MultiDimArray = @(
             (4,5,6),
             (24,30,36)
   )

Retrieving values from an array

If you type the array name, it will display all the items in that array:

$Array

The array number index starts at 0, so the first element will be accessed by this:

$Array[0]  

This is similar to displaying the fifth element in the $Array use:

$Array[4]

The last element of the array will be displayed by -1. So, the following example displays the last four items:

$Array[-4..-1]

We have used the range operator (..) here, which can also be used to display the elements from values 1 to 5 in the next example:

$Array[1..5]

The (+) operator combines the elements of the array. Use the following example to display the elements at index 0, 1, 3, and 5 to 8:

$Array[0,1,3+5..8]

The length property of the Count alias will display the number of items in an array:

 $Array.Count

While, ForEach, and For looping statements are used to display items in an array:

Foreach ($item in $Array) {$item}

The following For statement loops through and displays every third item in the array:

For ($n = 0; $n –le ($Array.length -1); $n+=3) {$Array[$n]}

The While statement is used to list the items in an array until the defined condition becomes false. The following example displays the items until the index position is less than 6:

$i=0
while($i -lt 5) {$a[$i]; $i++}

Now, let's look at manipulating items in arrays. The replacement of an item in an array can be done by using the assignment (=) operator and the index of the present item to be replaced. The following example replaces the fourth element with the value of 23:

$Array[3] = 23 

The SetValue method can also be used to replace the value of items in an array. The following example replaces the third value (index position 2) with a value of 34:

$Array.SetValue(34,2)

The elements are added to the array by the use of the += operator. The following example adds an element of 250 to the $Array array:

$Array +=250

The default array class in PowerShell does not provide an easy way to delete the elements of the array. Array elements can be copied over to another array and old arrays can be deleted though. In the following example, the $x array will consist of all the items from $array except the third and fifth items:

$x = $array[0,1,3 + 5..($array.length - 1)]

Multiple arrays can be combined in a single array by using a plus operator (+). The following example creates three arrays $a, $b, and $c and combines them to $x, which will now have all the elements of the three arrays:

        $a = 2,4,6,8,10
        $b = 1,3,5,7,9
        $c = 11,12,13,14,15
        $x= $a+$b+$c

Arrays can be deleted by assigning the value of $null to the array; for example, $array = $null.

For additional help on arrays, use the following cmdlet:

PS C:\> get-help about_arrays
lock icon The rest of the chapter is locked
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