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
About the Author
About the Reviewer

Using variables

In PowerShell, the data is stored, retrieved, and modified using the methods listed next. In this section, we are going to cover only variables:

  • A variable is used to store bits of information

  • Arrays are used to store information in an index

  • A Hash table uses key-value pair to store data

A variable is virtual storage to store information that will be utilized later in a script or the information that is the output of running a script. Variables can contain different data types such as text strings, objects, and integers. There are few special variables that you can use, which are defined in PowerShell.

The examples of some of the special variables are as follows:

Special Variable



This is the same as the $PS item. It contains the current object in the pipeline object and can be used in commands that perform an action on all or selected objects in a pipeline.


This contains the path of the user's' home directory, typically C:\Users\<UserName>.


This is an array that contains the errors in the current execution. The first element in the array ($Error[0]) will contain the most recent error.


This is an automatic variable that contains a NULL or empty value.

If you are looking for a full list of automatic variables available for use in Windows PowerShell, type the following:

PS C:\> get-help about_automatic_variables

In PowerShell, all the variable names must start with the $ character. Data is assigned to a variable using the = operator:

PS C:\> $Number = 25

There are the Set-Variable and Get-Variable cmdlets that can also be used to set and retrieve the values of the variable.

Here, the value 401B is passed to the variable ExamCode:

PS C:\> Set-Variable -Name ExamCode -Value 401B
PS C:\> Get-Variable ExamCode

A little bit about operators that are used for assignment and comparison of values assigned to variables.

Assignment operators

The following operators are used to assign values to variables and carry out numeric operations before the assignment:




A specific value is assigned to a variable using the equal to = operator


The variable value is increased by a specific number


The variable value is decreased by the number specified


This operator is used to multiply the value stored in a variable by a specified number


This divides the value by a specific number


In this case, the value stored in the variable is divided by the specified number and the modulus (remainder) is stored in the variable


This increases the value of variables by 1


This decreases the value of variables by 1

The example assigns the value of to the variable called $Book:

$Bookk = "Microsoft Exchange PowerShell Essentials"

In the previous example, the $Book variable is created as we have assigned the value to the variable using the assignment operator. If you look at the following example, the first statement creates a $x variable and assigns a value of 12. The second statement has changed its value to 34:

$x = 12
$ = 34

While scripting, there is a need to combine operations such as addition, subtraction, and assignment to variables. For example, the following statements will produce the exact same output:

$i += 3
$i = ($i +3)

Similarly, if you want to multiply first and then assign, use any of the following statements:

$n *= 4
$n = ($n * 4)

If you want to use a specific data type, you have to override how PowerShell stores values in variables. This can be achieved by strongly typed variables or casting them. For example, the first one is a variable that will only store integers and the second one will store strings:

[int]$Number = 12
[string]$Book = "Microsoft Exchange PowerShell Essentials"

Now, let's take a look at an example of what happens if we do not specify the correct datatype to a variable. Here, we have stored the value of 56 in the $x variable as a string. The next statement adds the value of 8 to the variable. The output will be displayed as a string and numeric operation will be performed. As a best practice, you should always cast your variables with the correct data type:

[string]$s =  56
$s += 8

You will not be able to recast a variable without changing its value. In our previous example, we have casted $Book as a string value and stored the value of Microsoft Exchange PowerShell Essentials in it. If you try to use the following statement, you will get an error message, which essentially means that you have to change the value before you cast the variable to a different data type:


Cannot convert value string to type System.Int32. Error—"Input string was not in a correct format."

At line:1 char:1
+ [int]$b 
+ ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

Now, let's use the Windows PowerShell help system to learn more about other assignment operators:

PS C:\> get-help about_assignment_operators

Comparison operators

These operators are used to compare values and match specific patterns. Here is the list of comparison operators available, and these operators are case insensitive. If you want to make these operators case sensitive or insensitive, precede c or i in front of the following operator:

  • eq

  • -ne

  • -gt

  • -ge

  • -lt

  • -le

  • -Like

  • -NotLike

  • -Match

  • -NotMatch

  • -Contains

  • -NotContains

  • -In

  • -NotIn

  • -Replace

Take a look at the following examples:

-eq—Equal to:

PS C:\> "apple" -eq "apple"
PS C:\> "apple" -eq "orange"

-ne—Not Equal to:

PS C:\> "apple" -ne "apple"
PS C:\> "apple" -ne "orange"

-gt—Greater than:

PS C:\> 5 -gt 8
PS C:\> 23,34,56 -gt 50

Like—Match using the wildcard character (*).

Take a look at the following examples:

PS C:\> "Windows PowerShell" -like "*shell"
PS C:\> "Windows PowerShell", "Server" -like "*shell"
          Windows PowerShell

Review the help for other comparison operators:

PS C:\> get-help about_comparison_operators