O365 Set-Distribution Group Email Address Policy

I am in the beginning stages of testing our bulk distribution group, contact and mailbox creations scripts for o365. In testing the script I am using for creating distribution groups, I received an error that the -emailaddresspolicyenabled $true couldn’t be found as a parameter. Everything I have looked at shows it being used with the set-distribution group command. Any assistance would be greatly appreciated.

$credential = Get-Credential

Import-Module MsOnline
Connect-MsolService -Credential $credential

$exchange365 = New-PSSession -ConfigurationName Microsoft.exchange -ConnectionUri https://outlook.office365.com/powershell-liveid -Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $exchange365 -DisableNameChecking

$distrogroup = Import-csv c:\temp\o365group.csv
$filename = "c:\temp\members.csv"


foreach ($i in $distrogroup) 
    {
           
      new-distributiongroup -Name $i.Name -Alias $i.Alias -primarysmtpaddress $i.Smtp -OrganizationalUnit $i.OrganizationalUnit -ManagedBy $i.manager 
                 
    }

    sleep 120

foreach($i in $distrogroup)

    {
        $groupname = $i.name
                   
        set-distributiongroup $groupname -RequireSenderAuthenticationEnabled $FALSE -EmailAddressPolicyEnabled $true 

    }

 sleep 240
    
if(Test-Path $filename)
{

$members = Import-csv c:\exchangetools\members.csv

foreach ($i in $members) 

{add-distributiongroupmember $i.groupname -Member $i.Name }

Hi

https://technet.microsoft.com/en-us/library/bb124955(v=exchg.160).aspx

EmailAddressPolicyEnabled
This parameter is available only in on-premises Exchange 2016.
$true Email address policies are applied to this recipient. This is the default value.

Seems like you need to remove it if you’re not using onpremise Exchange. Also why you have these sleep commands? You could add all these into one ForEach loop.

$credential = Get-Credential

Import-Module MsOnline
Connect-MsolService -Credential $credential

$exchange365 = New-PSSession -ConfigurationName Microsoft.exchange -ConnectionUri https://outlook.office365.com/powershell-liveid -Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $exchange365 -DisableNameChecking

$distrogroup = Import-csv c:\temp\o365group.csv
$filename = "c:\temp\members.csv"

foreach ($i in $distrogroup) {
           
      new-distributiongroup -Name $i.Name -Alias $i.Alias -primarysmtpaddress $i.Smtp -OrganizationalUnit $i.OrganizationalUnit -ManagedBy $i.manager 

    set-distributiongroup $i.Name -RequireSenderAuthenticationEnabled $FALSE

}
    
$members = Import-csv c:\exchangetools\members.csv

foreach ($i in $members) {
    add-distributiongroupmember $i.groupname -Member $i.Name
}

Regards

Jarkko

The sleep commands are there to provide enough time for the groups to be available to set. Without them I run into an issue where it can’t get the group to set the additional attributes. It takes a bit longer on the back end.