How Do I Handle Multi Value Parameters in New-DistributionGroup Script

Thanks in advance for any advice of wisdom here.

Can anyone advise of how i can amend the below script and have the DLs create and set if these parameter values are blank in the csv (captured from a previous export of on-prem to recreate in EOL). It seems these parameters have to have a value in the CSV that is used to feed into the script to run correctly (i tested with bogus info and it works fine when there are values here).


Script is as follows


        $importPath = Read-Host "Enter path to Group Export file..."
        $NewEolDLs = Import-Csv $importPath
        $NewEolDLs = $check

    ForEach ($DL in $NewEolDLs) 
        $NewDLName = "EOL" + $DL.DisplayName
        $ModEnabled = [System.Convert]::ToBoolean($DL.ModerationEnabled)
        $HiddenFAL = [System.Convert]::ToBoolean($DL.HiddenFromAddressListsEnabled)
        $ReqSendAuth = [System.Convert]::ToBoolean($DL.RequireSenderAuthenticationEnabled)

        # Create the new DL #
        New-DistributionGroup -Name $NewDLName -Alias $NewDLName -PrimarySmtpAddress $DL.PrimarySmtpAddress -DisplayName $NewDLName <#-ManagedBy $DL.Owner#> -MemberDepartRestriction $DL.MemberDepartRestriction`
        -ModerationEnabled $ModEnabled -RequireSenderAuthenticationEnabled $ReqSendAuth 

        # Update Settings on the new DL #
        Set-DistributionGroup -Identity $NewDLName <#-AcceptMessagesOnlyFrom $DL.AcceptMessagesOnlyFrom -AcceptMessagesOnlyFromDLMembers $DL.AcceptMessagesOnlyFromDLMembers #>`
        -GrantSendOnBehalfTo $DL.GrantSendOnBehalfTo -HiddenFromAddressListsEnabled $HiddenFAL -MemberDepartRestriction $DL.MemberDepartRestriction

I would just split the logic for creating the group from those attrubutes and have If statements checking if the value is null or not to update the group with a set-DistributionGroup command. You have two of them in the script already, if($importGroups) and if(!$check).

So just do If(!($ManagedBy)) { Set-DistributionGroup }

You can follow this up with if else statement to make it as granular as you want.

Note: Might want to hard code in a short sleep period if you do this. Sometimes if you do a set command right after creating the object, it wont find the object to modify

Consider using splatting and populating the parameters only if they’re not empty strings. Not tested:

    ForEach ($DL in $NewEolDLs) {
        $NewDLName = "EOL" + $DL.DisplayName

        # Create the new DL #

        $NewDistributionGroupParams = @{
            Name                               = $NewDLName 
            Alias                              = $NewDLName 
            PrimarySmtpAddress                 = $DL.PrimarySmtpAddress 
            DisplayName                        = $NewDLName 
            MemberDepartRestriction            = $DL.MemberDepartRestriction
            ModerationEnabled                  = [System.Convert]::ToBoolean($DL.ModerationEnabled)
            RequireSenderAuthenticationEnabled = [System.Convert]::ToBoolean($DL.RequireSenderAuthenticationEnabled)

        if ($DL.Owner -ne '') {

        New-DistributionGroup @NewDistributionGroupParams

        # Update Settings on the new DL #

        $SetDistributionGroupParams = @{
            Identity                      = $NewDLName
            GrantSendOnBehalfTo           = $DL.GrantSendOnBehalfTo 
            HiddenFromAddressListsEnabled = [System.Convert]::ToBoolean($DL.HiddenFromAddressListsEnabled)
            MemberDepartRestriction       = $DL.MemberDepartRestriction

        if ($DL.AcceptMessagesOnlyFrom -ne '') {
        if ($DL.AcceptMessagesOnlyFromDLMembers -ne '') {

        Set-DistributionGroup @SetDistributionGroupParams