Creating a Query-Based Distribution List

Exchange Server 2003 has introduced a new type of distribution list: the query-based DL. These are, as implied by name, distribution lists that are built on the fly based on a query; specifically an LDAP query against Active Directory.

This is an extremely powerful addition for Exchange, but you have to be careful because you can get into trouble with it. Unlike address lists, the query-based DL is resolved each time it is used with an actual LDAP query against Active Directory. This means that the query needs to be efficient. Used enough, a poorly designed query for the DL could severely impact Exchange and Active Directory performance. You will want to use indexed attributes and avoid bitwise operators, the NOT operator, and medial search strings as per normal Active Directory efficient programming guidelines. A medial search string is a search string that has a wildcard somewhere other than at the end of the string (e.g., *llen or j*e). See MSDN for more details (search for “Creating Efficient Active Directory Queries”).

Unlike every other object you can mail-enable, when you create a query-based DL you are not using the CDOEXM interface. However, when you create this object with ADUC, the Exchange Management tools must be loaded or the distribution list object will not be properly populated and will not function properly. When creating the object from script or command line, you directly set all of the Active Directory attributes of the msExchDynamicDistributionList object. The specific changes that need to be made are to the following attributes:

  • displayName
  • mailNickname
  • reportToOriginator
  • legacyExchangeDN
  • systemFlags
  • msExchDynamicDLBaseDN
  • msExchDynamicDLBaseFilter

Using a graphical user interface


  1. Open the ADUC snap-in.
  2. If you need to change domains, right-click on Active Directory Users and Computers in the left pane, select Connect to Domain, enter the domain name, and click OK.
  3. In the left pane, browse to the parent container of the new object, right-click on it and select New -> Query-based Distribution Group.
  4. Enter the group name and mail alias and click Next.
  5. Select the search base, enter the specifics of the filter, and then click Next.
  6. Verify the summary and click Finish.

Using a command-line interface

First, you need to create an LDIF file called add_qbdl.ldf with the following contents:

dn: CN=<QB DL Name>,<ParentDN> changetype: addcn: <QB DL Name>displayName: <QB DL Name>

objectClass: msExchDynamicDistributionList

mailNickname: <mail nickname>

legacyExchangeDN: <legacy Exchange DN>

msExchDynamicDLFilter: <LDAP Filter>

msExchDynamicDLBaseDN: <BaseDN>

reportToOriginator: TRUE

systemFlags: 1610612736

Replace <QB DL Name> with the name of the address list, <mail nickname> with the mail nickname, <legacy Exchange DN> with the appropriate legacy Exchange DN value, <LDAP Filter> with the specific LDAP filter you want to be used to determine group membership, <BaseDN> with the base distinguished name you want used in combination with the filter, and <ParentDN> with the distinguished name of the container you want the group created in. Then run the following command:

> ldifde -i -f add-qbdl.ldf

Using VBScript

' This code creates and mail enables a Query-Based Distribution List.
' ------ SCRIPT CONFIGURATION ------strParentDN  = "<Parent DN>"
' e.g., ou=groups,dc=rallencorp,dc=comstrGroupName ="<DL Name>"
' e.g., Sales Dept

strBaseDN = "<Base DN>"        ' e.g., ou=mail,dc=rallencorp,dc=com

strFilter = "<Filter>"         ' e.g., (&( department=sales)(homemdb=*))

strLegacyDN = "<Legacy DN of Recipients>" & "/cn=" & strGroupName

' e.g. /o=RALLENCORPMAIL/ou=First Administrative Group/cn=Recipients
' ------ END CONFIGURATION ---------

' Set Dynamic values

set objOU = GetObject("LDAP://" & strParentDN)

set objGroup = objOU.Create("msExchDynamicDistributionList","cn=" & _


objGroup.Put "msExchDynamicDLBaseDN", strBaseDN

objGroup.Put "msExchDynamicDLFilter", strFilter

objGroup.Put "displayName", strGroupName

objGroup.Put "mailNickname", strGroupName

objGroup.Put "legacyExchangeDN",strLegacyDN

' Set static values

objGroup.Put "systemFlags",1610612736

objGroup.Put "reportToOriginator",TRUE


Wscript.Echo "Successfully created query-based DL."