Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

HOW to Add multiple email addresses to Distribution Groups in Exchange online Office 365

I was working with one of my client on a automation solution with PowerShell for Exchange Online. I wanted to write a PowerShell script to add secondary alias email addresses to Distribution Groups in Exchange online Office 365.

Base of the script is Set-DistributionGroup cmdlet which does all the deadlift. I have added all the information in CSV files, secondary email address for group which will be configured in Distribution group as secondary email.

Microsoft Exchange Online office 365 PowerShell automation set-distrubutionGroup Group-object Import-csv secondary alias email primary email smtp SMTP add remove email addresses connect-exchangeonline import-module management.png

Download this script here Add-DistributionGroupEmailAddresses.ps1 or it is also available on github.com/janviudapi.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Import-Module -Name ExchangeOnlineManagement
Connect-ExchangeOnline

#$groupInfo = Get-DistributionGroup "NoReply DLP"
#Set-DistributionGroup "NoReply DLP" -EmailAddresses @{Add=$groupInfo.PrimarySmtpAddress,'j@test.com'}

$rawCsv = Import-CSV C:\Scripts\test\Test.csv
$csvData = $rawCsv | Group-Object-PropertyPrimarySmtpAddress

foreach ($csv in $csvData)
{
    $name=$csv.Name
    Write-Host "Working on Distribution Group - $name"-BackgroundColorDarkGreen

    #$groupInfo = Get-DistributionGroup $name  
    #$existingSecEmails =   $groupInfo.EmailAddresses.ForEach({$_.replace('smtp:', '')}) | Where-Object {$_ -cnotmatch 'SMTP:'}
    foreach ($secEmail in $csv.Group)
    {
        $secEmailAddress=$secEmail.secemail
        Write-Host"Adding secondary email / Alias - $secEmailAddress"#-BackgroundColor Yellow -ForegroundColor Black
        Set-DistributionGroup $csv.Group[0].PrimarySmtpAddress -EmailAddresses @{Add="smtp:$secEmailAddress"}
    }
}

Below is the csv file how it looks like. PrimarySmtpAddress is either name of distribution group or primary email id. In the secemail add the secondary email id. If there are multiple secondary smtp email ids add separate row (duplicate for primary address) and add secondary email there.

Office 365 Powershell Exchange Online microsoft cloud email server set-distribution list group microsoft 365 mail enabled security configuration portal csv file smtp primarysmtpaddress.png

After running the script I can check the results on Office 365 (Exchange Online) portal. In the Groups distribution list, there are aliases email ids are added.

Office 365 Powershell Exchange Online microsoft cloud email server set-distribution list group microsoft 365 mail enabled security configuration portal.png

Useful Articles
PowerShell slice array into groups of smaller arrays
Powershell web scrapping extract table from html
Powershell adding leading zeros to string or int
PowerShell convert string to base64 value
PowerShell Encode or Decode an WebURL
Create an interactive HTML report with PowerShell data

Go Back

Comment

Blog Search

Page Views

11275571

Follow me on Blogarama