Exchange Server 2010 Windows PowerShell: Working with Address Lists

0
111
5 min read

 

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here