Exchange Server 2010 Windows PowerShell: Working with Address Lists

Exclusive offer: get 50% off this eBook here
Microsoft Exchange 2010 PowerShell Cookbook

Microsoft Exchange 2010 PowerShell Cookbook — Save 50%

Manage and maintain your Microsoft Exchange 2010 environment with Windows PowerShell 2.0 and the Exchange Management Shell with this book and eBook

£22.99    £11.50
by Mike Pfeiffer | July 2011 | Cookbooks Enterprise Articles Microsoft

In this article by Mike Pfeiffer, author of Microsoft Exchange Server 2010 Windows PowerShell Cookbook, you'll learn how to create Address Lists and Hierarchal Address Books. This article has immediately-usable task-based recipes for managing and maintaining your Microsoft Exchange 2010 environment with Windows PowerShell 2.0 and the Exchange Management Shell for some address list management that can be automated through the shell.

In this article, we will cover the following:

  • Creating address lists
  • Exporting address list membership to a CSV file
  • Configuring hierarchical address books

 

Microsoft Exchange 2010 PowerShell Cookbook

Microsoft Exchange 2010 PowerShell Cookbook

Manage and maintain your Microsoft Exchange 2010 environment with the Exchange Management Shell and Windows PowerShell 2.0 using this book and eBook

        Read more about this book      

(For more resources on this subject, see here.)

Performing some basic steps

To work with the code samples in this article, follow these steps to launch the Exchange Management Shell:

  1. Log onto a workstation or server with the Exchange Management Tools installed
  2. Open the Exchange Management Shell by clicking on Start | All Programs | Exchange Server 2010
  3. Click on the Exchange Management Shell shortcut

The reader would benefit by reffering the previous article on Working with Distribution Groups.

Creating address lists

Just like dynamic distribution groups, Exchange address lists can be comprised of one or more recipient types and are generated using a recipient filter or using a set of built-in conditions. You can create one or more address list(s), made up of users, contacts, distribution groups, or any other mail-enabled objects in your organization. This recipe will show you how to create an address list using the Exchange Management Shell.

How to do it...

Let's say we need to create an address list for the sales representatives in our organization. We can use the New-AddressList cmdlet to accomplish this, as shown next:

New-AddressList -Name 'All Sales Users' `
-RecipientContainer contoso.com/Sales `
-IncludedRecipients MailboxUsers

How it works...

This example uses the New-AddressList cmdlet's built-in conditions to specify the criteria for the recipients that will be included in the list. You can see from the command that, in order for a recipient to be visible in the address list, they must be located within the Sales OU in Active Directory and the recipient type must be MailboxUsers, which only applies to regular mailboxes and does not include other types such as resource mailboxes, distribution groups, and so on. You can use the built-in conditions when you need to configure a basic filter for the list, and they can be easily edited from within the Exchange Management Console.

There's more...

When you need to create an address list based on a more complex set of conditions, you'll need to use the -RecipientFilter parameter to specify an OPATH filter. For example, the following OPATH filter is not configurable when creating or modifying an address list in EMC:

New-AddressList -Name MobileUsers `
-RecipientContainer contoso.com `
-RecipientFilter {
HasActiveSyncDevicePartnership -ne $false
}

You can see here that we're creating an address list for all the mobile users in the organization. We've set the RecipientContainer to the root domain, and, within the recipient filter, we've specified that all recipients with an ActiveSync device partnership should be included in the list.

You can create global address lists using the New-GlobalAddress list cmdlet.

You can combine multiple conditions in your recipient filters using PowerShell's logical operators. For instance, we can extend our previous example to add an additional requirement in the OPATH filter:

New-AddressList -Name MobileUsers `
-RecipientContainer contoso.com `
-RecipientFilter {
(HasActiveSyncDevicePartnership -ne $false) -and
(Phone -ne $null)
}

This time, in addition to having an ActiveSync device partnership, the user must also have a number defined within their Phone attribute in order for them to be included in the list.

If you need to modify a recipient filter after an address list has already been created, use the Set-AddressList cmdlet.

Exchange supports a various number of both common and advanced properties that can be used to construct OPATH filters, as shown in the previous example.

Exporting address list membership to a CSV file

When it comes to working with address lists, a common task is exporting the list of members to an external file. In this recipe, we'll take a look at the process of exporting the contents of an address list to a CSV file.

How to do it...

Let's start off with a simple example. The following commands will export the All Users address list to a CSV file:

$allusers = Get-AddressList "All Users"
Get-Recipient -RecipientPreviewFilter $allusers.RecipientFilter |
Select-Object DisplayName,Database |
Export-Csv -Path c:\allusers.csv -NoTypeInformation

When the command completes, a list of user display names and their associated mailbox databases will be exported to c:\allusers.csv.

How it works...

The first thing we do in this example is create the $allusers variable that stores an instance of the All Users address list. We can then run the Get-Recipient cmdlet and specify the OPATH filter, using the $allusers.RecipientFilter object as the value for the -RecipientPreviewFilter parameter. The results are then piped to the select-object cmdlet that grabs the DisplayName and Database properties of the recipient. Finally, the data is exported to a CSV file.

Of course, the given example may not be that practical, as it does not provide the e-mail addresses for the user. We can also export this information, but it requires some special handling on our part. Let's export only the DisplayName and EmailAddresses for each user. To do so, use the following code:

$allusers = Get-AddressList "All Users"
Get-Recipient -RecipientPreviewFilter $allusers.RecipientFilter |
Select-Object DisplayName,
@{n="EmailAddresses";e={$_.EmailAddresses -join ";"}} |
Export-Csv -Path c:\allusers.csv -NoTypeInformation

Since each recipient can have multiple SMTP e-mail addresses, the EmailAddresses property of each recipient is a multi-valued object. This means we can't simply export this value to an external file, since it is actually an object and not a simple string value. In the given command, we're using the Select-Object cmdlet to create a calculated property for the EmailAddresses collection. Using the -Join operator within the calculated property expression, we are adding each address in the collection to a single string that will be delimited with the semi-colon (;) character.

There's more...

The given method will work for any of the address lists in your organization. For example, you can export the recipients of the Global Address List (GAL) using the following code:

$GAL = Get-GlobalAddressList "Default Global Address List"
Get-Recipient -RecipientPreviewFilter $GAL.RecipientFilter |
Select-Object DisplayName,
@{n="EmailAddresses";e={$_.EmailAddresses -join ";"}} |
Export-Csv -Path c:\GAL.csv -NoTypeInformation

As you can see here, the main difference is that this time we are using the Get-GlobalAddressList cmdlet to export the default global address list. You can use this technique for any address list in your organization: just specify the name of the address list you want to export when using either the Get-AddressList or Get-GlobalAddress list cmdlets.

Microsoft Exchange 2010 PowerShell Cookbook Manage and maintain your Microsoft Exchange 2010 environment with Windows PowerShell 2.0 and the Exchange Management Shell with this book and eBook
Published: July 2011
eBook Price: £22.99
Book Price: £36.99
See more
Select your format and quantity:
        Read more about this book      

(For more resources on this subject, see here.)

Configuring hierarchical address books

Exchange 2010 SP1 introduced a new feature called the hierarchical address book (HAB). This allows users with Outlook 2010 to browse for recipients using an organizational hierarchy. The idea is that you can give your users the ability to search for recipients based on your organization's structure, versus the Global Address List which only provides a flat view. The configuration of a HAB can only be done using the Exchange Management Shell, and, in this recipe, we'll take a look at an example of how you can configure this feature in your organization.

How to do it...

  1. It is recommended that you create an OU in Active Directory to store the root HAB objects. You can create a new OU using your Active Directory administrations tools, or using PowerShell. The following code can be used to create an OU in the root of the Contoso domain called HAB:

    $objDomain = [ADSI]''
    $objOU = $objDomain.Create('organizationalUnit', 'ou=HAB')
    $objOU.SetInfo()

  2. Next, create a root distribution group for the HAB:

    New-DistributionGroup -Name ContosoRoot `
    -DisplayName ContosoRoot `
    -Alias ContosoRoot `
    -OrganizationalUnit contoso.com/HAB `
    -SamAccountName ContosoRoot `
    -Type Distribution

  3. Configure the Contoso distribution group as the root organization for the HAB:

    Set-OrganizationConfig -HierarchicalAddressBookRoot ContosoRoot

  4. At this point, you need to add subordinate groups to the root organization group. These can be existing groups or you can create new ones. In this example, we'll add three existing groups called Executives, Finance, and Sales to the root organization in the HAB:

    Add-DistributionGroupMember -Identity ContosoRoot -Member Executives
    Add-DistributionGroupMember -Identity ContosoRoot -Member Finance
    Add-DistributionGroupMember -Identity ContosoRoot -Member Sales

  5. Finally, we'll designate each of the groups as hierarchical groups and set the seniority index for the subordinate groups:

    Set-Group -Identity ContosoRoot -IsHierarchicalGroup $true
    Set-Group Executives -IsHierarchicalGroup $true -SeniorityIndex 100
    Set-Group Finance -IsHierarchicalGroup $true -SeniorityIndex 50
    Set-Group Sales -IsHierarchicalGroup $true -SeniorityIndex 75

  6. After this configuration has been completed, Outlook 2010 users can click on the Address Book button and view a new tab called Organization that will list our HAB:

Microsoft Exchange Server 2010 Windows PowerShell tutorial

How it works...

The root organization of a HAB is used as the top tier for the organization. Under the root, you can add multiple tiers by adding other distribution groups as members of this root tier and configuring them as hierarchical groups. This allows you to create a HAB that is organized by department, location, or any other structure that makes sense for your environment.

In order to control the structure of the HAB, you can set the SeniorityIndex of each sub group under the root organization. This index overrides the automatic sort order based on the DisplayName which would otherwise be used if no value was defined. This also works for individual recipients within each group. For example, you can set the SeniorityIndex on each member of the Executives group using the Set-User cmdlet:

Set-User cmunoz -SeniorityIndex 100
Set-User awood -SeniorityIndex 90
Set-User ahunter -SeniorityIndex 80

The users will be displayed in order, with the highest index number first. This allows you to further organize the HAB and override the default sort order if needed.

There's more...

You may notice that after configuring a HAB that Outlook 2010 users are not seeing the Organization tab when viewing the Address Book. If this happens, double check the Active Directory schema attribute ms-Exch-HAB-Root-Department-Link using ADSIEdit. The isMemberOfPartialAttributeSet attribute should be set to True. If it is not, change this attribute to True, ensure that this has replicated to all DCs in the forest, and restart the Microsoft Exchange Active Directory Topology service on each Exchange server. Of course, this is something you'll want to do during out of hours to ensure there is no disruption of service for end-users. After this work has been completed, Outlook 2010 users should be able to view the Organization tab in the Address Book.

Summary

In this article we covered how to create Address Lists and Hierarchal Address Books.


Further resources on this subject:


Microsoft Exchange 2010 PowerShell Cookbook Manage and maintain your Microsoft Exchange 2010 environment with Windows PowerShell 2.0 and the Exchange Management Shell with this book and eBook
Published: July 2011
eBook Price: £22.99
Book Price: £36.99
See more
Select your format and quantity:

About the Author :


Mike Pfeiffer

Mike Pfeiffer has been in the IT field for 15 years, and has been working on Exchange for the majority of that time. He is a Microsoft Certified Master and a former Microsoft Exchange MVP. These days he works at Microsoft as a Premier Field Engineer where he helps customers deploy and maintain Microsoft Exchange and Lync Server solutions. You can find his writings online at mikepfeiffer.net, where he occasionally blogs about Exchange, Lync, and PowerShell-related topics.

Books From Packt


Microsoft Data Protection Manager 2010
Microsoft Data Protection Manager 2010

Microsoft SQL Server 2008 R2 Administration Cookbook
Microsoft SQL Server 2008 R2 Administration Cookbook

Applied Architecture Patterns on the Microsoft Platform
Applied Architecture Patterns on the Microsoft Platform

Microsoft Visual Studio LightSwitch Business Application Development
Microsoft Visual Studio LightSwitch Business Application Development

Microsoft Silverlight 4 Data and Services Cookbook
Microsoft Silverlight 4 Data and Services Cookbook

Getting Started with Microsoft Application Virtualization 4.6
Getting Started with Microsoft Application Virtualization 4.6

Microsoft Enterprise Library 5.0
Microsoft Enterprise Library 5.0

Microsoft Forefront UAG 2010 Administrator's Handbook
Microsoft Forefront UAG 2010 Administrator's Handbook


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software