We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Create Microsoft 365 contact based on AD contact

February 12, 2021 Views: 2592

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
Leave a comment
Loading...

Got questions?

Support Questions & Answers