Office 365 – How to create a distribution list dynamic based on domain name

Hi everyone, today I wanted to write down a documentation for creating a distribution list updated dynamically when there is a new employee on board. Updating these list has always been challenging and I just thought this could be a good process for any Office 365 Admin.

1. Requirements

  • Microsoft Online Service Sign-in Assistant for IT Professionals RTW
  • SharePoint Online Management Shell

In our company we have a single tenant which host 5 different domain name. Office 365 makes it easy to manage all by one but it is also complicate when you want to filter information.

I believe Office 365 is not supporting this yet but we can use custom attributes for every user we want to select in our query.

2. Connection to Office 365 via PowerShell

$Cred = Get-Credential
Import-Module MSOnline
Connect-MsolService -Credential $cred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session

3. Checking Users

We need here to Export our Office 365 users.

$Date = (Get-Date -format “yyyyMMdd”)

Get-Mailbox | Select-Object Alias,CustomAttribute1,@{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_ -LIKE “SMTP:*”}}} | Sort | Export-Csv c:\OfficeUserList$Date.csv

Now we can open the list and see all Users with Alias, CustomAttribute and smtp address.

Open it via Excel and check the customeAttribute.

Untitled

4. Create custom attributes via PowerShell

Now, from this list it is easy to copy the first command and modify Alias name by the want we want to add in our future Distribution Group.

“username1″,”username2” ,””| set-mailbox –CustomAttribute1 “MyAttribute”

5. Creating a Distribution List (via Portal)

Access to your Office 365 portal and click on Admin > Exchange

DistribList1

Go to Recipients > Groups

DistribList2

Click on the “Plus” and select Dynamic Distribution group

DistribList3

Give it a name and some notes for people to understand the purpose of the list

DistribList3

Select options “Only the following recipient types” “Users with Exchange mailboxes” as we want to dynamically remove users who are not working anymore and add new users.

DistribList4

Select options “Only the following recipient types” “Users with Exchange mailboxes” as we want to dynamically remove users who are not working anymore and add new users.

DistribList6

Click ok and that’s done.

6. Creating a Distribution List (via PowerShell)

Use following command to create the Distribution group

New-DynamicDistributionGroup -Name “NameOfYourDistributionList” -RecipientFilter {(-not(RecipientTypeDetailsValue -eq ‘SharedMailbox’)) -and (-not(RecipientTypeDetailsValue -eq ‘RoomMailbox’)) -and (-not(RecipientTypeDetailsValue -eq ‘EquipmentMailbox’)) -and (Customattribute1-eq ‘YourAttribute‘)} –PrimarySmtpAddress NameOfYourDistributionList@MyDomain.com.au

In order to check and view the members of the dynamic distribution group, run the following comdlets

$WHAT = Get-DynamicDistributionGroup “NameOfYourDistributionList”
Get-Recipient -RecipientPreviewFilter $WHAT.RecipientFilter

DistribList7

This solution is not fully automate as you will have to set a custom Attribute when creating a new user but we can imagine the Office 365 Admin integrate it as a part of the workflow.

 

Leave a Reply

Your email address will not be published. Required fields are marked *