Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Microsoft Exchange Server PowerShell Essentials
  • Table Of Contents Toc
  • Feedback & Rating feedback
Microsoft Exchange Server PowerShell Essentials

Microsoft Exchange Server PowerShell Essentials

By : Banerjee
5 (3)
close
close
Microsoft Exchange Server PowerShell Essentials

Microsoft Exchange Server PowerShell Essentials

5 (3)
By: 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 (12 chapters)
close
close
11
Index

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

Description

$_

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.

$Home

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

$Error

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.

$NULL

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:

Operator

Description

=

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
$s
568

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:

[int]$Book 

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"
True
PS C:\> "apple" -eq "orange"
False

-ne—Not Equal to:

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

-gt—Greater than:

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

Like—Match using the wildcard character (*).

Take a look at the following examples:

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

Review the help for other comparison operators:

PS C:\> get-help about_comparison_operators
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Microsoft Exchange Server PowerShell Essentials
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon