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.
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,'[email protected]'} $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.
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.
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