Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
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 If statements

In this section, we will use the if conditional statement If to execute statements based on a specific condition test to be true. We can also use the If command to do tests on more than one condition or even if all the other conditions are evaluated to be false.

The following example shows the If statement syntax:

        if (<Condition1>) 
                {<Command Block 1>}
        [elseif (<Condition2>)
                {<Command Block 2>}]
        [else
                {<Command Block 3>}]

We will use the if command to check the status of the services running on the computer, and if the status is stopped, it will be displayed in red and the running services will be displayed in green. We will use a combination of pipeline, the foreach-object cmdlet, and the if statement.

PS C:\> get-service | foreach-object {if ($_.status -eq "stopped") {write-host -foregroundcolor red $_.Displayname}` else {write-host -foregroundcolor green $_.Displayname }} 

Let's take a look at another example where you would like to find whether a particular folder is created. We will use a Test-Path cmdlet, which will return true if it finds the folder and false if it does not. As I am using the statement on my Active Directory domain controller, the folder is not present under Program Files, and this is what you will see in the output:

PS C:\> $ExchangeFolder = "C:\Program Files\Microsoft\Exchange Server"
PS C:\> $FolderPresent = Test-Path $ExchangeFolder
PS C:\> if ($FolderPresent -eq $True) {Write-Host "Exchange installation Folder found on this Server"} else {Write-Host "Exchange installation folder not present"}
Exchange installation folder not present
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