Manage Distribution Group using PowerShell in Office 365 | Adding members to existing Distribution Group | Part 3#5 5/5 (1) 12 min read

The current article is that third article on the five-article series, which is dedicated to the subject of managing Distribution Group in Office 365 and Exchange Online based environment using PowerShell.
The article is dedicated to the subject of adding members to existing Distribution Group by using PowerShell.

Adding members to Distribution Group – prefix

In the next sections, we review two main scenarios, in which we need to add a “NEW member” to existing Distribution Group.

  • Scenario 1#2 – in this scenario, we want to add a “bulk” of users to a specific Distribution Group (many to one relationship).
  • Scenario 2#2 – in this scenario we want to add a specific user to “bulk” of Distribution Groups (one to many relationship).

The management task of adding new members to Distribution Group, is implemented by using the PowerShell cmdlet – Add-DistributionGroupMember.

Working with Distribution Group lists and Distribution Group member lists.

In the next section, we work with a concept which can be described as – “Group,” “list” or if we want to use the more technical term – “Array.”

This term defines a logical container, that “store objects (members)

In the specific scenario, we will use an “Array,” that contain user objects or Distribution Group objects.

The concept of Array

In our example, we create an “array” by using one of the following methods:

  • Option 1 – “array” that is created from the output from of a varies PowerShell commands.
    For example, the list of existing Distribution Group that we get from the
    PowerShell command – Get-DistributionGroup
  • Option 2 – using a file as a “source” for the “array”.
    In this case, we use a file (most of the time CSV file) that includes a list of Distribution Groups, a list of users and so on.
  • Option 3 – creating “array” by manually providing the “list” of the required Distribution Group or a list of Users. In this case, we manually provide the name of each Distribution Group or each user, separated by a comma.

The posable options for creating an array in PowerShell

Scenario 1 – Add a user (recipient) to a Distribution Group

This is a simple scenario, in which we wish to add a specific user to a Distribution Group.

In this example, we “write down” the name of the Distribution Group and the name of the “member” which will be added to the Distribution Group.

Add a user (recipient) to a Distribution Group

PowerShell command syntax

PowerShell command Example

Add a list of users to a specific Distribution Group

In the following section, we review examples of different scenarios, which their common denominator is that we need to add a “list of user” (array) to a specific Distribution Group.

This scenario can be described as – a relationship of – many to one.

Add a list of users to a specific Distribution Group

Scenario 1 – Add a “list of users” (multiple users) to Distribution Group | Import the “members list” from a CSV File

In this case, we want to add members to an existing Distribution Group, by importing the list of member’s from a CSV (Comma Separated Value) file.

In the current scenario, we export information about Office 365 users to a CSV file, by using the following PowerShell command:

PowerShell command Example

The CSV file structure and “logic”
The CSV file structure is based upon the concept of – “column” and “rows.”

Using a CSV file as a source of information for Distribution Group members -01

In our example, the CSV file includes a list of users. Notice that each user (each row) can have a couple of “identities.”

For example, each user has the following identities: Display name, Alias and Primary E-mail address.

Using a CSV file as a source of information for Distribution Group members -02

In our example, we “relate” to the user identity, by “fetching” the user identity stored in the
column named PrimaryE-mailaddress.

Technically speaking, we can choose another user’s identity such as “DisplayName” or “Alias.”

Using a CSV file as a source of information for Distribution Group members -03

Note – You can download the CSV files that we use in the article for a demonstration purpose.

The PowerShell command structure.
The PowerShell script that we use is based on the following syntax:

Import Distribution Group members from a CSV File | Command structure

PowerShell command syntax

  • Part 1#3 – In the first part of the PowerShell command, we import the information (the list of users) from the CSV file. In our example, we “declare” about a
    variable named – $UsersList that will serve as a logical container for the content of the CSV file.
  • Part 2#3 – In the second part of the PowerShell command, we use the “ForEach” PowerShell statement, for executing a “loop process.”
    The PowerShell “loop process” use an additional variable named – $User.
  • The $User variable, serve as “temporary” container that will store a specific user identity for each “cycle” in the loop process.
  • Part 3#3 – this is the “actual PowerShell command” that we use (Add-DistributionGroupMember) for adding each of the users that appear in the CSV file to a specific Distribution Group.

In our example, the Distribution Group name is – “Marketing USA

Notice that we relate to the Distribution Group member by using a combination of the $User variable that is “attached” to the specific column in the CSV file named – PrimarySmtpAddress, by using the following convention $User.PrimarySmtpAddress.

In other words, we instruct the PowerShell loop process to use the PrimarySmtpAddress as the user identity.

Using a CSV file as a source of information for Distribution Group members -04

Import Distribution Group members from a CSV File

PowerShell command Example

Scenario 2 – Add a “list of users” to Distribution Group | Users whom their department is equal to X.

In this scenario, our mission is to find all the users that their department is Sales, and these users to a Distribution Group named – “Sales worldwide”

In our example, the organization has a couple of sales departments, such as – Sales USA, Sales UK and so on.

To fulfill this requirement, we create the required list of users, by “executing” a PowerShell command that performs “filtered search.”

The PowerShell filtered search will look for all users, which their department is starting with the string “Sales*.”

We use the variable $SalesUsers for storing this information about the user’s list.

In the next step, we run a PowerShell loop process, which will use the information stored in the
variable – $SalesUsers and add each of the members stored in the $SalesUsers variable to the Distribution Group – Sales worldwide.

Add to Distribution Group all the users whom their department is Sales*

PowerShell command Example

Add a user (recipient) to Multiple Distribution Groups (bulk mode)

In the following section, we review examples of different scenarios, which their common denominator is – the need for adding a specific user to “many” or “multiple” (array) Distribution Group at the same time.

This scenario can be described as – a relationship of – one to many.

Add specific user as a member to multiple Distribution Groups

Scenario 1 | Add user (recipient) to multiple distribution groups | Manually providing the name of each Distribution Group.

In this section, I would like to review the scenario in which we want to add a specific user as a member to a list (array) of Distribution Groups

The information about the “destination Distribution Group” will be written as a part of the PowerShell command, and stored in a variable.

  • The user whom we want to add as a member to the various Distribution Group is – Brad.

The PowerShell command that we use, includes two parts:

  • Part 1#2 – in the first part of the PowerShell command, we define the PowerShell variable
    named – $GroupList, which serve as a “logical container” for the “array” of the Distribution Groups.
    In other words, define the list of the Distribution Group to which the user is going to be added.
  • Part 2#2 – In the second part of the PowerShell command, we use the “ForEach” PowerShell statement, for executing a “loop process.” The PowerShell “loop process” instructs PowerShell to:
    • “Get” the information stored in $GroupList
    • “Fetch” the name of the first Distribution Group in the list.
    • Add the user name as a member of the Distribution Group.

After the PowerShell loop, the process completes the task that relates to the “first Distribution Group” in the “list,” the PowerShell loop process starts the process all over with the “second Distribution Group” in the list until the “end” or the last name in the list.

Add user (recipient) to multiple distribution groups

PowerShell command syntax

PowerShell command Example

Scenario 2 | Add user (recipient) to multiple distribution groups | Using the PowerShell command for getting a list of all existing Distribution Groups.

In this scenario, we want to add a specific user to all the existing Distribution Groups.

To be able to fulfill this requirement, we need to define an “array,” that include all the available Distribution Groups.

The array of Distribution Group will be defined in the following way:

We will use a variable named – $ALLDistributionGroups ,that will store the output from the PowerShell command – Get-Distribution group -resultsize unlimited.
In the second part of the PowerShell command, we use the “ForEach” PowerShell statement, for executing a “loop process” on the results (the array the include the list of existing Distribution Groups).

An example of the PowerShell syntax that we use is:

Add user (recipient) to multiple distribution groups

PowerShell command Example

Scenario 3 | Add user (recipient) to multiple distribution groups | read the information about the Distribution Group list from a CSV file.

In this example, the information about the “list of Distribution Groups,” is stored in a CSV file. Technically speaking, the CSV file can include many additional “information columns,” beside of the specific column that includes the Distribution Groups names.

In our specific example, the column that includes the Distribution Group names named – “GroupName.”

It’s important to mention that the column header name is not a “predefined name” or a mandatory name.

Instead, the column name is just an arbitrary name, that we choose. The only “restriction” is that the name of the header will not contain any spaces.

The specific column that contain the names of the Distribution Groups

Note – You can download the CSV files that we use in the article for a demonstration purpose.
  • In the first part of the PowerShell command, we define a variable named – $DistributionGroupsList, that stores the content of the CSV file (the CSV file that contains the list of the Distribution Groups).
  • The second part of the PowerShell command will “access” the information stored in the
    column – GroupName.

Add user to multiple distribution groups listed in a CSV file

PowerShell command Example

Scenario 4 | Add user (recipient) to multiple distribution groups | get a list of Distribution Group that was created in the last X hours.

In the following example, we want to add the user Brad as a member of all the Distribution Groups, that was created in the last 48 hours.

  • Part 1#2 – in the first part of the PowerShell command, we define the PowerShell variable
    named – $AllNewDistributionGroups.
    The $AllNewDistributionGroups variable will store the output from a PowerShell query that “fetch” all the Distribution Group that their “creation time” is less or equal to the last 40 hours.
  • Part 2#2 – In the second part of the PowerShell command, we use the “ForEach” PowerShell statement, for executing a “loop process” on the results.
    The PowerShell command will relate to each of the Distribution Group by using the $Group variable that will use the “name” property of the Distribution Group object.

Add user to distribution groups that was created in the last 48 hours

PowerShell command Example

Scenario 5 – Add user to Distribution Group only if the user is not already a member of the Distribution Group

In the following scenario, we wish to verify if a specific user is a member of a Distribution Group, if the user is not a member of the Distribution Group, we want to add the user as a member of the Distribution Group.

Scenario description

  • The user whom we want to check is – Bob.
  • The Distribution Group name is – Sales France.

To be able to fulfill this requirement, we can use the following PowerShell command

PowerShell command Example

Manage Distribution Group using PowerShell in Office 365 | Article series index

Now it’s Your Turn!
It is important for us to know your opinion on this article

Print Friendly, PDF & Email

Related Post

Please rate this

Eyal Doron on EmailEyal Doron on FacebookEyal Doron on GoogleEyal Doron on LinkedinEyal Doron on PinterestEyal Doron on RssEyal Doron on TwitterEyal Doron on WordpressEyal Doron on Youtube
Eyal Doron
Share your knowledge.
It’s a way to achieve immortality.
Dalai Lama

Leave a Reply

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