The script creates a Microsoft 365 (Office 365) contact based on an existing Active Directory contact.
PowerShell
# Connect to Exchange Online
$Context.CloudServices.ConnectExchangeOnline()
# Create contact
try
{
New-MailContact -Name "%name%" -ExternalEmailAddress "%mail%" -DisplayName "%displayName%" -FirstName "%firstname%" -LastName "%lastname%" -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occurred when creating a contact. Error: " + $_.Exception.Message, "Warning")
}