In Exchange Online you can create a Dynamic Distribution List, but it takes time to get the list populated when you create it in the web portal. A faster way is to use PowerShell. In this example, I have created a string that use 2 different company names because I had to do this for a client, but at the same time I saw that the list also got polulated by the meeting rooms, so I needed to exclude meeting rooms. "Company" can be replaced with "Department" if you prefer to use this value in your organization.
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline
# Create a Dynamic Distribution List
New-DynamicDistributionGroup -Name "All Users in Enterprise" -IncludedRecipients "MailboxUsers" -PrimarySmtpAddress "All_Users@CompanyName.com" -DisplayName "All Users in Enterprise" -ManagedBy "Big.Boss@CompanyName.com"
# Set values a Dynamic Distribution List
Set-DynamicDistributionGroup -Identity "All_Users@CompanyName.com" -RecipientFilter "(RecipientType -eq 'UserMailbox') -and (-not(RecipientTypeDetailsValue -eq 'RoomMailbox')) -and ((Company -eq 'Contoso') -or (Company -eq 'SpinToys'))"
# Change Managed By
Set-DynamicDistributionGroup -Identity "All_Users@CompanyName.com" -ManagedBy "Assistant@CompanyName.com"
# Check the members
$group = Get-DynamicDistributionGroup -Identity "All_Users@CompanyName.com"
Get-Recipient -RecipientPreviewFilter $group.RecipientFilter -OrganizationalUnit $group.RecipientContainer | Select-Object DisplayName, RecipientType | Sort-Object DisplayName
Comments