There comes a time, especially working in schools, where you have to add a lot of contacts and groups at once to Office 365.
This is where Powershell comes in. What would take you days through the Office 365 admin page will take you an hour or two using Powershell.
Preparing Contacts
The first thing you need to do is prepare a CSV for Powershell to pull contact data from. Each contact needs an email address and a name as follows:
ExternalEmailAddress | Name | FirstName | LastName |
---|---|---|---|
firstperson@gmail.com | John Smith | John | Smith |
secondperson@hotmail.com | Jane Doe | Jane | Doe |
Connecting to Powershell
Now you need to connect to Exchange Online using a Powershell session but first you want to enable the running of scripts. Open Powershell and type:
Set-ExecutionPolicy RemoteSigned
$UserCredential = Get-Credential
Enter your Office 365 Admin details at the prompt. Then continue below.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
You should now be connected to your Exchange Online account. Enter the following to import your Contacts CSV file. The below code makes the assumption that your first is in the C:\ drive and is called contacts.csv. Rename as appropriate.
Import-Csv C:\contacts.csv|%{New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName}
This completes the importing of new contacts into Exchange Online.
Bulk Importing into Groups
If it’s a new group you can create it in Powershell:
New-DistributionGroup -Name "GROUP_NAME" -Type "Security"
Then create a CSV file for each group you need:
Name |
---|
John Smith |
Jane Doe |
Back in Powershell enter the following:
$contacts=get-Mailcontact
Import-CSV C:\FILE_NAME.csv | ForEach {Add-DistributionGroupMember -Identity "GROUP_NAME" -Member $_.Name}
Replacing FILE_NAME with the filename of your CSV file AND GROUP_NAME with the desired group.
And that’s it. Please do comment below if you know of any further ways to reduce the time it takes to create groups and contacts in bulk.