Reader small image

You're reading from  Mastering Windows PowerShell Scripting (Second Edition) - Second Edition

Product typeBook
Published inOct 2017
PublisherPackt
ISBN-139781787126305
Edition2nd Edition
Right arrow
Author (1)
 Brenton J.W. Blawat
Brenton J.W. Blawat
author image
Brenton J.W. Blawat

Brenton Blawat has been a successful entrepreneur, strategic technical advisor, and engineer who has a passion for the procurement of technology in organizations. He is business-centric, while being technology-minded, and has had many years of experience bridging the gap between technical staff and decision makers in organizations. Brenton prides himself on his ability to effectively communicate to a diverse audience and provide strategic direction for small and large organizations alike. Throughout his career, he has worked for a multitude of Fortune 500 organizations, and specializes in delivery automation and workflow optimizations.
Read more about Brenton J.W. Blawat

Right arrow

Chapter 7. Branching and Looping

A branch in a script or command is created every time an if/switch statement or loop is added. The branch represents a different set of instructions. Branches can be conditional, such as one created by an if statement, or unconditional, such as a for loop.

As a script or command increases in complexity, the branches spread out the same as the limbs of a tree.

In this chapter, we are going to cover the following topics:

  • Conditional statements
  • Loops

Conditional statements


Statements or lines of code may be executed when certain conditions are met. PowerShell provides if and select statements for this purpose.

If, else, and elseif

An if statement is written as follows; the statements enclosed by the if statement will execute if the condition evaluates to true:

if (<condition>) { 
    <statements> 
} 

The else statement is optional and will trigger if all previous conditions evaluate to false:

if (<first-condition>) { 
    <first-statements> 
} else { 
    <second-statements> 
} 

The elseif statement allows conditions to be stacked:

if (<first-condition>) { 
    <first-statements> 
} elseif (<second-condition>) { 
    <second-statements> 
} elseif (<last-condition>) { 
     <last-statements> 
}

The else statement may be added after any number of elseif statements.

Execution of a block of conditions stops as soon as a single condition evaluates to true. For example, both the first...

Loops


Loops may be used to iterate through collections, performing an operation against each element in the collection; or to repeat an operation (or series of operations) until a condition is met.

Foreach

The foreach loop executes against each element of a collection using the following notation:

foreach (<element> in <collection>) { 
    <body-statements> 
} 

For example, the foreach loop may be used to iterate through each of the processes returned by Get-Process:

foreach ($process in Get-Process) { 
    Write-Host $process.Name 
}    

If the collection is $null or empty, the body of the loop will not execute.

For

The for loop is typically used to step through a collection using the following notation:

for (<intial>; <exit condition>; <repeat>){ 
    <body-statements>
 } 

Initial represents the state of a variable before the first iteration of the loop. This is normally used to initialize a counter for the loop.

The exit condition must be true as long as...

Summary


In this chapter, we have explored if and switch statements.

Each of the different loops, foreach, for, dountil, dowhile, and while, has been introduced.

In the Chapter 8, Working with .NET, we will explore working with the .NET Framework.

 

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Windows PowerShell Scripting (Second Edition) - Second Edition
Published in: Oct 2017Publisher: PacktISBN-13: 9781787126305
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.
undefined
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 €14.99/month. Cancel anytime

Author (1)

author image
Brenton J.W. Blawat

Brenton Blawat has been a successful entrepreneur, strategic technical advisor, and engineer who has a passion for the procurement of technology in organizations. He is business-centric, while being technology-minded, and has had many years of experience bridging the gap between technical staff and decision makers in organizations. Brenton prides himself on his ability to effectively communicate to a diverse audience and provide strategic direction for small and large organizations alike. Throughout his career, he has worked for a multitude of Fortune 500 organizations, and specializes in delivery automation and workflow optimizations.
Read more about Brenton J.W. Blawat