cancel
Showing results for 
Search instead for 
Did you mean: 
Brian_Baldock

Comparing the members of a distribution list to the members of an Office 365 group or Team

Hello everyone, cycling through members of a distribution list and comparing them to the members of another group is a relatively simple thing to solve in PowerShell but took me some time to wrap my head around in Power Automate.

Below, I broke the process down into multiple steps. The end goal of this project is to be able to detect new members of a distribution list and perform specific actions for those new users such as adding them to a team, send them a custom email or so many other possibilities.

In the use case below, my objective is to analyze the members of a distribution list which is populated by an HR process and automatically add new members to a specific Team, and send them a custom welcoming email with details.

 

Create the flow
  1. Go to My flows and Click New:
  2. I went with a Scheduled flow so that this process is completely hands-off.
  3. Name your flow and your schedule, this can be modified later so specify a date way off in the future, this will give you enough time to test it out.
    CreateFlow.png

 



 

Get the distribution list [DL] members
  1. Add an action and type “Azure AD” in the search box. Select Azure AD and scroll down in the actions section to “Get group Members”. This will be the action that gets the Distribution List members.
  2. Now you will need to enter your Group ID (Object ID), this is your distribution lists unique identifier. You can find this identifier in the Azure AD Admin Center under the group object. Or by using PowerShell and searching for the group:

Connect to Microsoft 365 PowerShell

https://aka.ms/AAacemw

Get Azure AD Group Object ID

https://aka.ms/AAac8zh

 

I suggest renaming the action to something more obvious like “Get Distribution List Members” instead of the default “Get group members”. This will help with the flow later.

GetDLMembers.png

 

 

Initialize a variable to store the DL members
  1. Next, you’ll need a variable to trim down the output of the “Get Distribution List Members” action:
  2. Create a new step and type “Initialize Variable”.
  3. Specify the name of the variable (remember, rename it something obvious) in this case we’ll name it “DLMembers”.
  4. Select “Type” and specify “Array”.
  5. See the screenshot to the right for the flow as it should look up until this point.

VarDLMembers.png

 

 

 

Append the DL members to the variable
  1. Now we need to populate the variable “DLMembers” with the User ID of each member of the distribution list. To do this we’ll cycle through each member in the output of “Get Distribution List Members” and inject the “User ID” value into the “DLMembers” variable.
  2. Create a new step and select “Control” and “Apply to each”.
  3. Under “Select an output from previous steps” select “add a dynamic value”.
  4. Under “Get Distribution List Members” or whatever you named that first step, select “Group Members”.
  5. Now click “Add an action”, search for “Append to array variable” and select “Append to array variable”.
  6. Under name select “DLMembers” and value select “add a dynamic value”. Under “Get Distribution List Members” select “Group Members Id”.
  7. This would be a good time to click “Save” 

Important note: This action will list all distribution list members. This includes all nested groups. To detect a nested group, you will need additional steps. These will be outlined in a future post.

Note: Again, as suggested previously it would be easier if you rename the actions above. See screenshot below, for example. I renamed “Apply to each” to “Foreach DL Member append ID to DLMembers” and “Append to array variable” to “Append to array variable DLMembers”.

AppendDLMembers.png

 

 

Get the current Team or O365 Group members
  1. The next step will get a list of all the members of the specified Team or Office 365 Group.
  2. Create a new step and type “Office 365 Groups” and “List group members”.
  3. For “Group ID” specify the Team or O365 Group you want to compare the DL Members to.
  4. Click “Show advanced options” and set the “Top” to 999 (which is the maximum)

Note: Now, keep in mind some of the limitations for Teams or O365 Groups. There can only be a maximum of 10,000 members in a Team. https://aka.ms/AAactar This function allows only listing a maximum of 999 members.

GetTeamMembers.png

 

 

 

 

Add a new variable for Team Members
  1. Create a new variable to store the Team Members.
  2. Specify the name of the variable (remember, rename it something obvious) in this case we’ll name it “TeamMembers”.
  3. Select “Type” and specify “Array”.
  4. See the screenshot for the flow as it should look up until this point.

VarTeamMembers.png

 

 

 

 

Append the Team members to the variable
  1. Now we need to populate the variable “TeamMembers” with the User ID of each member in the Team. To do this we’ll cycle through each member in the output of “Get Team Members” and inject the “User ID” value into the “TeamMembers” variable.
  2. Create a new step and select “Control” and “Apply to each”.
  3. Under “Select an output from previous steps” select “add a dynamic value”.
  4. Under “Get Team Members” or whatever you named that first step, select “Value”.
  5. Now click “Add an action”, search for “Append to array variable” and select “Append to array variable”.
  6. Under name select “TeamMembers” and value select “add a dynamic value”. Under “Get Team Members” select “User Id”.
  7. This would be a good time to click “Save” 

AppendTeamMembers.png

 

 

 

 

 

 

 

 

Run the comparison and do stuff
  1. Now we need to compare the DLMember variable to the TeamMember variable. We need to detect if a User Id in the DLMember variable already exists in the TeamMember variable. The trick in this type of loop is the “Current item” dynamic value.
  2. Create a new step and add a new “Control” and select “Apply to each”. Rename this loop to “Foreach DLMember check if already in Team”.
  3. The “Select an output from previous steps” add dynamic value and under variable select “DLMembers”.
  4. Click “Add and action” and select “Control” and “Condition”.
  5. “Add dynamic content” under Variables select “TeamMembers” and select “contains” and then select “Add dynamic content” and under “Foreach DLMember check if already in Team” select “Current Item”

CompareAndDoStuff.png

 

 

 

 

Here is an image of the entire flow:

FullFlow.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I hope this proves useful.

 

Brian Baldock

Comments

Pardon my ignorance, but is an Office 365 distribution list the same as an Outlook distribution list. I have been trying to find a way to use Power Automate to get the names of all members of an MS Outlook distribution list

Meet Our Blog Authors
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • I am a Microsoft Business Applications MVP and a Senior Manager at EY. I am a technology enthusiast and problem solver. I work/speak/blog/Vlog on Microsoft technology, including Office 365, Power Apps, Power Automate, SharePoint, and Teams Etc. I am helping global clients on Power Platform adoption and empowering them with Power Platform possibilities, capabilities, and easiness. I am a leader of the Houston Power Platform User Group and Power Automate community superuser. I love traveling , exploring new places, and meeting people from different cultures.
  • Blog site: https://ganeshsanapblogs.wordpress.com/ MCT | SharePoint, Microsoft 365 and Power Platform Consultant | Contributor on SharePoint StackExchange, Techcommunity
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • I am the Owner/Principal Architect at Don't Pa..Panic Consulting. I've been working in the information technology industry for over 30 years, and have played key roles in several enterprise SharePoint architectural design review, Intranet deployment, application development, and migration projects. I've been a Microsoft Most Valuable Professional (MVP) 12 consecutive years and am also a Microsoft Certified SharePoint Masters (MCSM) since 2013.
  • Big fan of Power Platform technologies and implemented many solutions.
  • Passionate #Programmer #SharePoint #SPFx #M365 #Power Platform| Microsoft MVP | SharePoint StackOverflow, Github, PnP contributor
  • Web site – https://kamdaryash.wordpress.com Youtube channel - https://www.youtube.com/channel/UCM149rFkLNgerSvgDVeYTZQ/