Ever run into a user request where they need you to update a distribution list with users that are not on your system? No Problem you say, only to discover its a excel spread sheet with 100 or more people. AGH….
PowerShell to the rescue.
Here is a script I used to create and add mail contacts to a 365 Distribution list in mass. This code assumes 2 things. 1st you have csv with with columns named ‘CONTACT NAME’ and ‘E-MAIL’, 2nd you have already connected to Exchange online.
$csvpath = “C:\PATH TO\Users.csv”
$list = “EMaillist”
$people = Import-Csv $csvpath
foreach($person in $people){
$check = $null
$check = Get-MailContact $person.’E-MAIL’
if($check -eq $null)
{
New-MailContact -name $person.’CONTACT NAME’ -ExternalEmailAddress $person.’E-MAIL’
Add-DistributionGroupMember -Identity $list -Member $person.’E-MAIL’
} Else{
Add-DistributionGroupMember -Identity $list -Member $person.’E-MAIL’
}
}
Thats it, simple easy. In this script we are checking if the there is already a contact, if no we go and create it and add it to our group. If the contact is already in Exchange we simply just add it.