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


*Note: Everyone is the alias of the Dynamic Group


Then, run Get-DynamicDistributionGroup -Identity everyone | fl Name,RecipientFilter


 Name           : everyone

((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(RecipientTypeDetailsValue -eq 'GuestMailUser')))




this is the filter used to create the DDG.

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 -identity nb-allusers | 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')) -and (-not(RecipientTypeDetailsValue -eq 'GuestMailUser')))}


To check your work in PowerShell

$FTE = Get-DynamicDistributionGroup “Everyone"

Get-Recipient -RecipientPreviewFilter $FTE.RecipientFilter


Any further exclusions can then be added using the 365 portal by setting the customattribute1 field to the value exclude.


    

Afterwards close the session

Remove-PSSession $Session