1

I'm using the following code to get all members in a group.

Get-DistributionGroupMember -Identity "Marketing USA"

The problems is, some of groups have more than 1000 members. So instead of loading all the records, is there a better way I can filter the result to see only the members I need.

I would like to search by Primary Email and Display Name.

My Exchange versions are 2010 and 2013.

Richard
  • 6,812
  • 5
  • 45
  • 60
Azri Zakaria
  • 1,324
  • 5
  • 23
  • 52

3 Answers3

0

For both Exchange 2010 and 2013 you can search by email address by creating a list of email addresses in regex form separated by |. Then use -imatch to match the email addresses you need. Note: This will search both primary and secondary address.

$EmailAddress = [regex]"Username1@Domain.com|Username2@Domain.com|Username3@Domain.com"
Get-DistributionGroupMember -Identity "Marketing USA" | Where { $_.EmailAddresses.SmtpAddress -imatch $EmailAddress }

Then to searching by display name you can all so use regex in the same way separating the names by |.

$DisplayName = [regex]"FirstName1 LastName1|FirstName2 LastName2|FirstName3 LastName3"
Get-DistributionGroupMember -Identity "Marketing USA" | Where { $_.DisplayName -imatch $DisplayName }

With this method of filtering you can also do partial searches. If you only put in the first name of the user you will get back all users with that name. The same goes will the email address search.

Richard
  • 6,812
  • 5
  • 45
  • 60
  • Already try and seems like the performance is poor. Is it's true the record will upload in the memory first after that it will filter? – Azri Zakaria Aug 25 '16 at 08:29
  • `Get-DistributionGroupMember` outputs the members one by one into the `Where` cmdlet which will filter the results, so no more than 1 record is sorted in Memory. I think you will struggle to find a quicker way. – Richard Aug 25 '16 at 09:13
0

According to documentation by default the result size is limited to 1000. You will need to include -ResultSize Unlimited e.g. Get-DistributionGroupMember -Identity "Marketing USA" -ResultSize Unlimited to return more than that.

JBaldridge
  • 103
  • 1
  • 2
  • I don't want to load all the records, I want to search members only. – Azri Zakaria Aug 25 '16 at 08:29
  • If the person you want to match is person 1001 in the distribution group and you only look through the first 1000 people in that group you are never going to see the person you are looking for. I don't want to steal what @Richard wrote but you will have to combine `-ResultSize Unlimited` in with what he answered to search against all members in your distribution list if there are greater than 1000 members in that list. – JBaldridge Aug 25 '16 at 15:20
0

A distribution group is also an AD group. With the Get-ADGroup cmdlet you also get the possibility to filter the search in a much more effective way.

For example:

Get-ADGroup -Filter {proxyaddresses -like "SMTP:address@DOMAIN.COM"} (or use the mail attribute if that is equal to the primary email address in your org)

or

Get-ADGroup -Filter {displayname -eq "MyDL"} -Properties *

To use this cmdlet you might need to load the activedirectory ps module with 'import-module activedirectory' first in your powershell session.

Erwin
  • 51
  • 3