How To Exclude a User from a Dynamic Distribution Group in Office 365
How To Exclude a User from a Dynamic Distribution Group

First open PowerShell and connect to the exchange system.

  1. $LiveCred = Get-Credential (then enter your Office 365 email address and password in the box that pops up)
  2. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic –AllowRedirection
  3. Import-PSSession $Session

 

Then, run Get-DynamicDistributionGroup -Name “Group Name” | fl Name,RecipientFilter

 Name           : Full Time Employees

RecipientFilter : ((((((((((((((((((((((RecipientType -eq ‘UserMailbox’)))) -and (-not(Name -like ‘SystemMailbox{*’)))) -and (-not(Name -like ‘CAS_{*’)))) -and (-not(RecipientTypeDetailsValue -eq ‘MailboxPlan’)))) -and (-not(RecipientTypeDetailsValue -eq ‘DiscoveryMailbox’)))) -and (-not(RecipientTypeDetailsValue -eq ‘PublicFolderMailbox’)))) -and (-not(RecipientTypeDetailsValue -eq ‘ArbitrationMailbox’)))) -and (-not(RecipientTypeDetailsValue -eq ‘AuditLogMailbox’)))) -and (-not(RecipientTypeDetailsValue -eq ‘AuxAuditLogMailbox’)))) -and (-not(RecipientTypeDetailsValue -eq ‘SupervisoryReviewPolicyMailbox’)))) -and (-not(Name -like ‘SystemMailbox{*’)) -and (-not(Name -like ‘CAS_{*’)) -and (-not(RecipientTypeDetailsValue -eq ‘MailboxPlan’)) -and (-Not(RecipientTypeDetailsValue -eq ‘DiscoveryMailbox’)) -and (-not(RecipientTypeDetailsValue -eq ‘PublicFolderMailbox’)) -and (-not(RecipientTypeDetailsValue -eq ‘ArbitrationMailbox’)) -and (-not(RecipientTypeDetailsValue -eq ‘AuditLogMailbox’)) -and (-Not(RecipientTypeDetailsValue -eq ‘AuxAuditLogMailbox’)) -and (-not(RecipientTypeDetailsValue -eq ‘SupervisoryReviewPolicyMailbox’)))

This is the filter used to create the DDG in the.

Copy the entire filter argument into notepad and add: (-not (CustomAttribute1 -like ‘exclude’) to the filter. Be aware the entire filter may be enclosed in parentheses and move the closing bracket as needed.

The final code will look something like:

Get-DynamicDistributionGroup Full Time Employees | Set-DynamicDistributionGroup -recipientfilter {((RecipientType -eq ‘UserMailbox’) -and (-not (CustomAttribute1 -like ‘exclude’)) -and (-not(Name -like ‘SystemMailbox{*’)) -and (-not(Name -like ‘CAS_{*’)) -and (-not(RecipientTypeDetailsValue -eq ‘MailboxPlan’)) -and (-not(RecipientTypeDetailsValue -eq ‘DiscoveryMailbox’)) -and (-not(RecipientTypeDetailsValue -eq ‘PublicFolderMailbox’)) -and (-not(RecipientTypeDetailsValue -eq ‘ArbitrationMailbox’)) -and (-not(RecipientTypeDetailsValue -eq ‘AuditLogMailbox’)) -and (-not(RecipientTypeDetailsValue -eq ‘AuxAuditLogMailbox’)) -and (-not(RecipientTypeDetailsValue -eq ‘SupervisoryReviewPolicyMailbox’)))}

To check your work in PowerShell

$FTE = Get-DynamicDistributionGroup “Full Time Employees”

Get-Recipient -RecipientPreviewFilter $FTE.RecipientFilter