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
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Using filters and exporting data


In order to understand how powerful Windows PowerShell is, you need to look at the filtering capabilities it offers. With an understanding of objects and pipelining, you can retrieve the exact information you are looking for. The Usage of Pipeline section talks about how pipelines can be used to pass one or more objects, which are the output of one command, and feed them into another cmdlet. In Windows PowerShell, there are commands that will help you to refine and get the desired output. Some of these cmdlets that we will look in this section are Where-Object, Sort-Object, Select-Object, and Select-String.

The Where-Object cmdlet helps you to filter the output returned by other cmdlets. For example, the Get-service cmdlet without any parameter retrieves information about the services installed on the computer on which it is called:

Now, let's filter the output of Get-Service to find the list of services that are stopped:

Windows PowerShell supports the following logical operators that we are going to use in our next example of filtering the output of a Get-Service cmdlet:

Operator

Description

-and

This is true only when both statements are true

-or

This is true when either or both statements are true

-xor

This is true if one condition is true and the other one is false

-not

The statement followed by this operator is negated

!

This is the same as the –not operator

We will now find services that are running whose name starts with Windows using the –and operator:

By default, Windows PowerShell will sort the output of commands using an alphabetical order. You can then use the Sort-object cmdlet to sort it using any way you like. For example, the following example will first sort the output of the Get-Process cmdlet by the Process ID and then the CPU time:

At times, you might need to filter your results based on certain specific property names. This is where the Select-Object cmdlet comes in handy. It selects the stated properties of a single or group of objects. You can use Select-Object to select objects that are unique in a group of objects It supports parameters such as First, Last, Skip, and Index. You can select one or multiple object properties using the –Property parameter. Let's take an example of the output of the Get-Process cmdlet, and let's say you are troubleshooting a performance issue on a server and are only interested to know about the CPU time and process name properties. The output of the Get-Process cmdlet will be piped to Select-Object with the –property parameter set to the process name and CPU. The Format-list cmdlet will format the output displayed in the console:

PS C:\> Get-Process | Select-Object -Property Processname, CPU | format-list

Now, let's use another parameter called –Unique of Select-Object. This will select a single member from a number or objects with same properties and values. In the following example, you will notice that the –Unique parameter is case sensitive and reports "c" and "C" as unique.

Another useful filtering cmdlet is Select-String, used to select text patterns in strings and files. If you are familiar with UNIX shells, it is used as Grep or Findstr in Windows. It can display all the matches or stop after the first match depending on the usage of the cmdlet. You can search files of Unicode text using this command as well.

First, we will create a file using the redirection operator (>) to store the list of services running on a server to a text file:

PS C:\> Get-Service > service.txt

Now, we will find the text "Network" and "Windows" to get a list of all the services that have the word in it. We will then use the –notmatch parameter to list all the services except this word:

Note that the search performed here is a case insensitive search; you can use the —case sensitive parameter to make it case sensitive, which means you won't get any output if you use the parameter with the previous command as there are no matches found.