Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Microsoft Exchange Server PowerShell Essentials
  • Table Of Contents Toc
  • Feedback & Rating feedback
Microsoft Exchange Server PowerShell Essentials

Microsoft Exchange Server PowerShell Essentials

By : Banerjee
5 (3)
close
close
Microsoft Exchange Server PowerShell Essentials

Microsoft Exchange Server PowerShell Essentials

5 (3)
By: Banerjee

Overview of this book

PowerShell has become one of the most important skills in an Exchange administrator's armory. PowerShell has proved its mettle so widely that, if you're not already starting to learn PowerShell, then you're falling behind the industry. It isn't difficult to learn PowerShell at all. In fact, if you've ever run commands from a CMD prompt, then you'll be able to start using PowerShell straightaway. This book will walk you through the essentials of PowerShell in Microsoft Exchange Server and make sure you understand its nitty gritty effectively. You will first walk through the core concepts of PowerShell and their applications. This book discusses ways to automate tasks and activities that are performed by Exchange administrators and that otherwise take a lot of manual effort. Microsoft Exchange PowerShell Essentials will provide all the required details for Active Directory, System, and Exchange administrators to help them understand Windows PowerShell and build the required scripts to manage the Exchange Infrastructure.
Table of Contents (12 chapters)
close
close
11
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
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Microsoft Exchange Server PowerShell Essentials
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon