Powershell – Getting users from dynamic distribution group Exchange 2007/2010

exchange-2007exchange-2010powershell

I've found article at Microsoft claiming that this powershell query would give me a list of users in a dynamic distribution group that is defined more or less like this:

Image

The code to list:

$MarketingGroup = Get-DynamicDistributionGroup "Marketing Group"
Get-Recipient -RecipientPreviewFilter $MarketingGroup.RecipientFilter -OrganizationalUnit $MarketingGroup.OrganizationalUnit

So I modified it a bit:

$members = Get-DynamicDistributionGroup -Identity "dynamic group"
Get-Recipient -RecipientPreviewFilter $members.RecipientFilter -OrganizationalUnit $members.OrganizationalUnit | select Displayname,PrimarySmtpAddress > membersall.txt

but the problem is my query (and Microsoft for that matter) takes only part of equation into consideration. It takes the radio/checkboxes choice but it seems to be skipping the Container the users are in (even thou $members.OrganizationalUnit should do the trick). It seems to return everyone with Users with Exchange mailboxes that are in the chosen container but it also takes the people which are in default Users OU.

So how to modify the query to display only the ones within the recipient container chosen without the default Users OU.

Best Answer

You are using the wrong property for OrganizationalUnit The following should work:

$MarketingGroup = Get-DynamicDistributionGroup "Marketing Group"
Get-Recipient -RecipientPreviewFilter $MarketingGroup.RecipientFilter -OrganizationalUnit $MarketingGroup.RecipientContainer