Book Image

Microsoft Exchange Server PowerShell Essentials

By : Biswanath Banerjee
Book Image

Microsoft Exchange Server PowerShell Essentials

By: Biswanath 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 (17 chapters)
Microsoft Exchange Server PowerShell Essentials
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Using loops


In this section, we will review the usage of loops in Windows PowerShell. Every scripting language has multiple ways to loop through an array of items and execute a block of statements. Windows PowerShell has a variety of looping mechanisms described next.

The Do loop

Here is another way to run a statement block based on a conditional result. The Do loop is used with While and Until keywords to run a script block along with a conditional statement. Both the loop Do-While and Do-Until run once before the condition is evaluated. The command block is executed in a Do-While loop as long as the condition is true, whereas a Do-Until loop runs until a condition evaluates to be true.

The syntax of Do-While and Do-Until is the same. The following shows the syntax of the Do-While statement:

          do {<command block>} while (<condition>)
          do {<command block>} until (<condition>)

Examples are as follows:

Count to 5—In the following example, the statement is to use an increment operator with a $a variable and increment the value by 1 (as listed earlier in the assignment operators section of this chapter), and the condition is evaluated to be true until the value of a is less than 5. Here is how we will use the loop in a single line in PowerShell:

PS C:\> Do { $a++ ; Write-Host $a } while($a -ne 5)

The previous statement can be used in a different way where a carriage return is used instead of a semi-colon:

 PS C:\>  Do { $a++ 
          Write-Host $a
        } while($a -ne 5) 

The Do-Until loop executes the statement list until the condition is false. Once the condition evaluates to be true, the loop exits.

In the following example, the script block will write the output of the variable to the console and increment it by 1, and the process is repeated until the condition, which is the value, becomes greater than 5 is evaluated to be false. Once $Count exceeds number 5, changing the condition to true, the loop exits:

PS C:\>  $Count = 1
do {Write-Host $Count; $Count++}
until ($Count -gt 5)

ForEach loops

The ForEach statement is used to iterate through a series of items in a collection. A typical collection is to traverse through the items in an array. You can specify within the ForEach loop a command or a command block against each item in the array:

Syntax is as follows:

                     foreach ($<Element> in $< group of items>){Command Block>}

The following ForEach loop displays the values of the $Number array.

In Windows PowerShell, the entire ForEach statement should be in one line for execution. Here is an example:

$Numbers = "1","2","3","4","5"; foreach ($No in $Numbers) {Write-Host $No}

If you are using a PowerShell script file, you can use the ForEach statement using multiple lines in a .ps1 file:

  $Numbers = "1","2","3","4","5"
  foreach ($No in $Numbers)
  {
  Write-Host $No
  }

In the previous example, the $Numbers array is used to store values from 1 to 5. The ForEach statement loops through each of the items in the $Number array. In the first time, it sets the $No variable with value 1, second time with value 2, and so on. The loops exit when the value of 5 is reached.

We will use ForEach in the remaining chapters with cmdlets that return a collection. For example, we will use ForEach to iterate through each of the file/directory returned by Get-ChildItem in this case:

foreach ($item in Get-ChildItem)
        {
              Write-Host $item
        }

Loop through an array of strings:

 $Fruit = @("Apple","Peach","Banana","Strawberry") foreach ($fruit in $fruit) { "$fruit = " + $fruit.length }
The following example displays all the numbers in the series except 2:
foreach ($num in 1,2,3,4,5) { if ($num -eq 2) { continue } ; $num }

While loops

The While loop is used to execute statements in the command block as long as the condition is evaluated to be true.

Here is the syntax of a While loop:

while (<condition to be evaluated>) {<Command Block>}

In a while statement, the condition is evaluated to be either true or false. As long as the condition is true, the command block is executed, which can be a combination of more commands running through each iteration of the loop.

The following example lists numbers from 1 to 9 as the value of variable is $value is evaluated to be true until its value is equal to 9:

  while ($value -ne 9)
  {
  $value++
  Write-Host $value
  }

You can write the same command in PowerShell in one line as follows:

while($value -ne 9){$value++; Write-Host $value}

Note that there is a semi-colon (;) that separates the two commands in the command block. The first one increments the value of $value by 1, and the second command writes the output of $value to the console.