Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PowerShell for Office 365

You're reading from   PowerShell for Office 365 Automate Office 365 administrative tasks

Arrow left icon
Product type Paperback
Published in Jul 2017
Publisher Packt
ISBN-13 9781787127999
Length 222 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Prashant G Bhoyar Prashant G Bhoyar
Author Profile Icon Prashant G Bhoyar
Prashant G Bhoyar
Martin Machado Martin Machado
Author Profile Icon Martin Machado
Martin Machado
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. PowerShell Fundamentals FREE CHAPTER 2. Managing Office 365 with PowerShell 3. Azure AD and Licensing Management 4. Managing SharePoint Online Using PowerShell 5. Managing Exchange Online Using PowerShell 6. Script Automation 7. Patterns and Practices PowerShell 8. OneDrive for Business 9. PowerShell Core

Using the if and where statements

When writing scripts, we have to implement business logic that will shape the process. The if and where statements allow us to define logical conditions. For example, if you would like to compare two numbers, you can use the if and else statements and, based on the comparison, take appropriate action.

Conditional statements are the building blocks of any programming and scripting language.

If a certain condition is true, we can run a block of code. The syntax of an if...else is as follows:

if (<test1>)  
{<statement list 1>}
elseif (<test2>)
{<statement list 2>}
else
{<statement list 3>}

Here's an example:

$a = 6;
if( $a -eq 5){
Write-Host "Variable a is equal to 5"
}
elseif( $a -eq 4){
Write-Host "Variable a is equal to 4"
}
else
{
Write-host "Variable a is not equal to 5 and 4"
}

The output of this script will be Variable a is not equal to 5 and 4.

We can use a combination of the if...else statements where in the if block we check for a condition: if that condition is true, then we execute a block of code, and if the condition is not true, then we execute another block of code. Sometimes, we can have more than one expected outcome and we can use multiple elseif conditions. The comparison operator -eq returns Boolean values (true or false). If the outcome of the comparison is true, then the associated block of code is executed. Since it is a Boolean value, we can use the reverse logic as well.

We have a lot of comparison operators available in PowerShell:

  • -eq: Equal to
  • -ne: Not equal to
  • -gt: Greater than
  • -ge: Greater than or equal to
  • -lt: Less than
  • -le: Less than or equal to
  • -like: Wildcard match
  • -notlike: Does not match wildcard
  • -match: Regular expression matching
  • -notmatch: Does not match regular expression pattern
  • -contains: Collection contains item
  • -notcontains: Collection does not contain item
  • -in: Item is in a collection

We can use multiple comparison operators in a single if statement. This helps you implement complex scenarios.

You can have multiple if statements or even use nested if statements.

We can Where-Object cmdlet to filter data return by other cmdlets. For example, if we would like to find out the processes running on a computer with the name svcHost we can use the Where-Object cmdlet with the Get-Process cmdlets as shown below.

Get-Process | Where-Object {$_.name -contains "svcHost"}
You have been reading a chapter from
PowerShell for Office 365
Published in: Jul 2017
Publisher: Packt
ISBN-13: 9781787127999
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 €18.99/month. Cancel anytime