How to use Exchange Management Shell's Filter command
Learn how to use the Filter command and operators in Exchange Server 2007's Exchange Management Shell to obtain more granular search results.
Understanding how to use the basic syntax of Exchange Management Shell commands in Exchange Server 2007 will allow...
Continue Reading This Article
Enjoy this article as well as all of our content, including E-Guides, news, tips and more.
administrators to perform tasks that may not be possible through the Exchange Management Console. The problem is that Exchange Management Shell commands can return thousands of search results. Fortunately, you can use the Filter command to control which search results are displayed.
The Get-Mailbox command in the Exchange Management Shell can return thousands of results, depending on how many mailboxes reside in your Exchange Server 2007 organization. Displaying these results on one screen wouldn't be possible, nor would finding the Exchange 2007 mailbox data you need.
Fortunately, the Exchange Management Shell facilitates performing queries so that only desired data is displayed. The primary mechanism to narrow down search results is the Filter command, which allows you to construct queries so that Exchange Server compares various object attributes against specified values.
For example, to display a list of all users who worked in the IT department, use the Get-User command, which works similar to the Get-Mailbox command. You then would use the user's department as the filtering criteria. The actual command would be:
Get-User –Filter {Department –eq 'IT'}
In this command, Get-User lists all user accounts. The –Filter command informs Exchange Server that you want to filter the results based on particular criteria. The actual filtering criteria are enclosed in brackets. For example, Department is the attribute that we are filtering. The –EQ denotes that the attribute should be equal to a particular value. The value that we are searching for is enclosed in apostrophes at the end of the command ('IT'). This particular Filter command informs Exchange Server to display any user who works in the IT department.
There are several other operators aside from –EQ that can be used with the Filter command. Other useful operators are –OR, –AND and –LIKE.
- The –OR operator allows you to test for multiple conditions and create queries in which either this or that is true. For example, suppose that you wanted to create a query based on title, and you wanted Exchange Server to display users with the title of either Manager or Supervisor. The syntax for such a command is nearly identical to the previous one, except that we're appending another condition to test for with the –OR operator. The actual command is:
Get-User –Filter {(Title –eq 'Supervisor') –or (Title –eq 'Manager')}
In this command, the query is still enclosed in brackets, but because we're using the –OR operator, the query has two separate parts. Each of these parts is enclosed in parenthesis within the brackets. Incidentally, you aren't limited to using one instance of the –OR operator. You can construct a query using multiple operators.
- The –AND operator can help you create a query in which multiple conditions must be met, such as a query that showed only managers who work in the IT department. To do so, we would construct a query similar to the previous one, but use the –AND operator instead of the –OR operator. The actual command looks like this:
Get-User –Filter {(Title –eq 'Manager') –and (Department –eq 'IT')}
This command won't return a result unless a user is a manager and works in the IT department. The –AND operator also can be used in conjunction with the –OR operator to create more complex queries. For example, you would use the following command to obtain a list of every user who works in the IT department and is either a manager or a supervisor:
Get-User –Filter {((Title –eq 'Supervisor') –or (Title –eq 'Manager')) –And (Department –eq 'IT')}
The thing that is different about this command is that we must use an extra set of parenthesis. Notice that the conditions that are being evaluated in the –OR portion of the command are enclosed by parenthesis as usual, but there is an extra set of parenthesis surrounding the entire –OR statement. This is because the extra parenthesis causes the entire –OR statement to be evaluated before the results are joined to the –AND statement.
- In the –OR and –AND commands, we performed queries on titles of Manager and Supervisor. But these titles may not be realistic. This is where the –Like operator can be used. This operator can help you find users whose titles include the word Manager, but that word isn't necessarily the entire title. The command looks like:
Get-User –Filter {Title –like '*Manager*'}
The word Manager is enclosed in apostrophes, but asterisks are placed on both ends of the word. This tells Exchange Server that you want to see any results containing the word Manager. The asterisks indicate the optional presence of text at the beginning or end of the word. Like the other operators, you can use multiple instances of the –Like operator in conjunction with –AND, –OR and –EQ operators.
Available attributes
In addition to operators that can be used with the –Filter command, administrators can also create filters on a variety of attributes. First, however, you must understand which attributes are available.
The Get-Mailbox -? command can be used to view the full syntax of the Get-Mailbox command. The same principle applies to the Get-User command. Enter the Get-User -? command to view the full syntax of the Get-User command. Unfortunately, displaying the command's syntax doesn't display a list of attributes onto which you can base filters.
![]() |
||||
|
![]() |
|||
![]() |
Generally, you can create filters based on nearly any attribute viewable through the Exchange Management Console. The trick to creating a filter is knowing the name that the Exchange Management Shell assigns to the attribute that you want to filter.
There are a few tricks that you can use to find attribute names. One trick is to open the Exchange Management Console and navigate through the console tree to:
Recipient Configuration -> Mailbox
When you do, the Details pane will display all of the Exchange 2007 mailboxes in the organization. There also is a Create Filter button located above the list of mailboxes. If you click this button, you can use the GUI to construct a filter similar to ones we've been creating with the Exchange Management Shell.
Some of the attribute names that the Create Filter window uses, shown in Figure 3, are an exact match for those used within the Exchange Management Shell; others are not. While some attributes listed in the console are comprised of two or more words, you often can take attribute names as listed, remove the spaces and use the resulting attribute name in the Exchange Management Shell.
Figure 3. Some attribute names can be obtained through the Exchange Management Console. (Click on image for enlarged view.)
If needed, you can also look up attribute names on Microsoft's website. This page provides a separate listing for attributes that were added in Exchange Server 2007 SP1.
A PRIMER ON THE EXCHANGE 2007 EXCHANGE MANAGEMENT SHELL
Home: Introduction
Part 1: How to use the Exchange Management Shell command syntax
Part 2: How to use the Exchange Management Shell Filter command
Part 3: Control query results with the EMS Format command
Part 4: How to test Exchange Management Shell commands
ABOUT THE AUTHOR: |
![]() Brien M. Posey, MCSE, has previously received Microsoft's MVP award for Exchange Server, Windows Server and Internet Information Server (IIS). Brien has served as CIO for a nationwide chain of hospitals and was once responsible for the Department of Information Management at Fort Knox. As a freelance technical writer, Brien has written for Microsoft, TechTarget, CNET, ZDNet, MSD2D, Relevant Technologies and other technology companies. You can visit Brien's personal Web site at http://www.brienposey.com. |