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 mail-enabled group in Exchange Online based on AD group

The script creates a mail-enabled group in Exchange Online based on an existing Active Directory group and sets the email address for both groups. The script does not synchronize group members.

Parameters:

  • $domainName - Specifies the domain name (e.g. example.com) that will be used in the e-mail adress for the groups.
  • $primarySmtpAddress - Specifies the e-mail adress that will be set for the groups.
Edit Remove
PowerShell
$domainName = "domain.com" # TODO: modify me
$primarySmtpAddress = "%name%@$domainName" # TODO: modify me

# Get group type
$groupType = $Context.TargetObject.Get("groupType")
if ($groupType -band [Softerra.Adaxes.Interop.Adsi.ADS_GROUP_TYPE_ENUM]::ADS_GROUP_TYPE_SECURITY_ENABLED)
{
    $type = "Security"
}
else
{
    $type = "Distribution"
}

try
{
    # Connect to Exchange Online
    $session = $Context.CloudServices.CreateExchangeOnlinePSSession()
    Import-PSSession $session -AllowClobber -DisableNameChecking -CommandName "New-DistributionGroup"
    
    # Create new group
    try
    {
        New-DistributionGroup -Name "%name%" -PrimarySmtpAddress $primarySmtpAddress -Type $type -ErrorAction Stop
    }
    catch
    {
        $Context.LogMessage("An error occurred when creating group in Exchange online. Error: " + $_.Exception.Message, "Warning")
        return
    }
    
    # Update AD group email
    $Context.TargetObject.Put("mail", $primarySmtpAddress)
    $Context.TargetObject.SetInfo()
}
finally
{
    # Close the remote session and release resources
    if ($session) { Remove-PSSession $session }
}

Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers