Home > How to use Exchange Management Shell's Filter command
Crash Course:
EMAIL THIS LICENSING & REPRINTS

How to use Exchange Management Shell's Filter command

07 May 2008 | Brien Posey

Exchange Server tips, tutorials and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google

Understanding how to use the basic syntax of Exchange Management Shell commands in Exchange Server 2007 will allow 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.
More Exchange Management Shell resources:

Manage Exchange 2007 public folders with the EMS

How to create Exchange 2007 journaling rules with Exchange Management Shell 

Methods for moving mailboxes and public folders to Exchange 2007

Migrating resource mailboxes from Exchange 2003 to Exchange 2007

The Exchange Management Shell for Exchange Server 2007

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.

Exchange Server 2007 Exchange Management Console 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.

ABOUT THE AUTHOR:   
Brien M. Posey, MCSE
Brien M. Posey, MCSE, is a Microsoft Most Valuable Professional for his work with Exchange Server, and has previously received Microsoft's MVP award for 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 website at www.brienposey.com.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
Microsoft Exchange Server 2007
Re-enable Exchange Server 2007 remote streaming backups in SP1
How to use the Exchange Management Shell command syntax
Secure Edge Transport servers using the Security Configuration Wizard
What is Windows Server 2008's impact on an Exchange 2007 migration?
Is it time to upgrade users' Windows Mobile devices?
Customizing Outlook Web Access (OWA) in Exchange Server 2007
Managing an Exchange 2007 Cluster Continuous Replication (CCR) setup
Monitor mail flow with the Exchange Server 2007 Queue Viewer tool
Create a global Safe Senders List in Exchange 2007 to filter spam
A primer on Exchange 2007 server roles
Microsoft Exchange Server 2007 Research

Microsoft Exchange Server Scripts and Programming
How to use the Exchange Management Shell command syntax
Fix OWA message size limit issue after Exchange 2007 SP1 upgrade
Automatically save Microsoft Outlook email attachments to disk
Can an admin create out-of-office messages from ESM or AD?
Customize Exchange 2007 mail quota messages from the command line
Windows PowerShell RC2 ready to go
Creating a customized welcome email in Outlook Web Access
Scripting solutions for Exchange Server administrators
Microsoft Outlook script: Banish blank email subject lines
Microsoft Outlook security workaround for administrators and developers

Exchange Server Administration Tips
Re-enable Exchange Server 2007 remote streaming backups in SP1
Why Exchange ActiveSync fails with NAT firewalls
How to use the Exchange Management Shell command syntax
Email archiving and e-discovery best practices for Microsoft Exchange
Secure Edge Transport servers using the Security Configuration Wizard
Is it time to upgrade users' Windows Mobile devices?
Top 10 Microsoft Exchange Server 2003 registry hacks
Monitor mail flow with the Exchange Server 2007 Queue Viewer tool
Use Performance Monitor to detect Exchange 2003 message queue problems
Exchange 2007 memory and hardware configuration best practices

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
application program interface  (SearchExchange.com)
event sink  (SearchExchange.com)
synchronous groupware  (SearchExchange.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsWebcastsWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2004 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts