Script Repository


Create Microsoft 365 contact based on AD contact

February 12, 2021
1781

The script creates a Microsoft 365 (Office 365) contact based on an exeisting Active Directory contact.

Edit Remove
PowerShell
try
{
    # Connect to Exchange Online
    $session = $Context.CloudServices.CreateExchangeOnlinePSSession()
    Import-PSSession $session -AllowClobber -DisableNameChecking
    
    # 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")
    }
}
finally
{
    # Close the remote session and release resources
	if ($session) { Remove-PSSession $session }
}

Comments ( 0 )
No results found.
Leave a comment

Related Scripts