How It Works
Connect to Exchange Online
Connect-ExchangeOnline -UserPrincipalName [email protected] -ShowProgress $trueCode language: PowerShell (powershell)
Opens an authenticated session to Exchange Online using the Exchange Online PowerShell module. The -ShowProgress flag gives you a progress bar during connection, which is helpful on slow links.
Create the Distribution Group
New-DistributionGroup -Name "Distribution Group 1" -Alias "distrogroup1" -PrimarySmtpAddress "[email protected]"Code language: PowerShell (powershell)
Creates the group with a display name, an alias used for internal routing, and the primary SMTP address that external senders will use. All three matter. Skip the alias and Exchange will generate one for you, which is usually ugly.
Build the Members Array
$members = @(
"[email protected]",
...
)Code language: PowerShell (powershell)
Defines the list of members as a plain string array. Adding or removing someone is as simple as editing this list before you run the script.
Add Members in a Loop
foreach ($member in $members) {
Add-DistributionGroupMember -Identity "Distribution Group 1" -Member $member
}Code language: PowerShell (powershell)
Iterates through each address and adds it to the group. The -Identity value must match the -Name you passed to New-DistributionGroup exactly, or the loop will fail on every iteration.
Disconnect
Disconnect-ExchangeOnline -Confirm:$falseCode language: PowerShell (powershell)
Closes the session cleanly. Skipping this leaves a dangling remote session and can cause issues if you run other Exchange scripts in the same terminal session later.
Usage
Requirements
- Module: Exchange Online Management module. Install it with:
Install-Module -Name ExchangeOnlineManagementCode language: PowerShell (powershell)
- Permissions: The account you connect with needs the Distribution Groups management role in Exchange Online. A global admin works, but scope it down if you can.
Running the Script
Swap in your actual values before running:
UserPrincipalNameon the connect line: your admin accountName,Alias, andPrimarySmtpAddressonNew-DistributionGroup- The addresses in
$members - The
-Identityvalue in the loop must match the-Nameexactly
.Create-DistributionGroup.ps1Code language: PowerShell (powershell)
You’ll be prompted to authenticate when Connect-ExchangeOnline runs. MFA is supported.
Caveats
- No error handling. If an address in the array doesn’t correspond to a valid mailbox or contact in your tenant,
Add-DistributionGroupMemberwill throw an error and move on. You won’t get a summary of what failed. Wrap the loop body in atry/catchif you need that.
- Group already exists. If you run this twice with the same name,
New-DistributionGroupwill error out. The loop will still run and attempt to add members to the existing group. That may or may not be what you want.
- Propagation delay. After creation, the group may not be immediately usable for sending mail. Exchange Online can take a few minutes to fully provision a new distribution group.
- Membership limits. Exchange Online caps distribution group membership at 100,000 members. Not a concern for most use cases, but worth knowing if you’re doing something large.
- The
-Identitystring is case-insensitive but must be an exact name match. If you rename the group after creation and forget to update the loop, everyAdd-DistributionGroupMembercall will fail.
Full Script
# Connect to Exchange Online
Connect-ExchangeOnline -UserPrincipalName [email protected] -ShowProgress $true
# Create the new distribution group
New-DistributionGroup -Name "Distribution Group 1" -Alias "distrogroup1" -PrimarySmtpAddress "[email protected]"
# Add members to the distribution group
$members = @(
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
)
# Change Distribution Group 1 to the name of your distribution list above
foreach ($member in $members) {
Add-DistributionGroupMember -Identity "Distribution Group 1" -Member $member
}
# Disconnect from Exchange Online
Disconnect-ExchangeOnline -Confirm:$falseCode language: PowerShell (powershell)