Adding External Email addresses to a new distribution group

HI, I’m pretty new to powershell and I have had Microsoft guys on the phone and they dont seem to be able to do what I need to achieve.

I have a CSV file with 580 email addresses and I want to bulk import them to my new group. Initially I was told that we would need to import them into contacts and from there, we would import them to the new group.

Then on another call, I was told that -no -we could import them straight into the new group. So I spent the next 3 phone calls with Microsoft running scripts that didnt work and one that partially worked.

Here’s the script I ran last: Import-Csv C:\Users\test\Desktop\adultwellbeing.csv | foreach{Add-DistributionGroupMember -Identity AdultHealthAndWellBeing -Member $_.alias}

Here is the repeated error I get for each email address (email address varies in each line) and the error is repeated over and over again. I am assuming its looking at each of the addresses in the csv but not sure why its not finding what it needs - is it because they need to be in contacts first? Any info would be appreciated. Thanks.

Couldn’t find object “mail@rncyc.com”. Please make sure that it was spelled correctly or specify a different object.

 

Paul, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “CODE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

Assuming is a quite unprofessional way of solving problems. :wink:

You should always read the help for the cmdlets you’re about to use. Read it completely including the examples to learn how to use it.

Add-DistributionGroupMember

For the parameter -Member you can read:

The Member parameter specifies the recipient that you want to add to the group. A member can be any mail-enabled recipient in your organization. You can use any value that uniquely identifies the recipient. For example:

Name
Alias
Distinguished name (DN)
Canonical DN
Email address
GUID

HI, Thanks for the welcome and the advice.

So, on the basis that the member parameter relates to any mail enabled user within the organization, could this possibly be the reason for the failure as the csv file contains all external (non organizational) addresses. The email addresses wont exist in the org.

Do they have to be added into contacts first or is there a mechanism by which I can import them directly to a new but empty distribution group?

 

Thanks

 

 

 

 

… yes :wink:

… yes :wink:

… not that I’m aware of.

When it comes to adding external addresses to a Distribution Group, you have always needed to create contacts in Exchange in order to do this.
Fortunately, it is relatively straightforward to accomplish using PowerShell and something that I have done a number of times when working at an MSP.
Allow me to explain.

The contacts need to be created so that the external addresses appear in the Address Book. Once you have created contacts, these can be added to a distribution group in the same way you add your internal domain accounts.

The CSV file used in this example contains one column with a header of “EmailAddress”. The following code should perform the tasks you require. I have left the option -Whatif in the code so that this can be run without making any changes to your system. It will however, show you what the outcome would be without it. It is necessary to iterate through your CSV processing each email address in turn, which is achieved using the Foreach loop.

# First, we convert the CSV to an object
$Contacts = Get-Content -Path C:\PATHTOFILE\DistList.csv | ConvertFrom-Csv
# Next we loop through the list
foreach ($Contact in $Contacts) {
# Create a new contact
New-MailContact -Name "$Contact.EmailAddress" -ExternalEmailAddress "$Contact.EmailAddress" -Whatif
# Create a new contact
Add-DistributionGroupMember -Identity "AdultHealthAndWellBeing" -Member "$Contact.EmailAddress" -Whatif
# Optional but you can also hide these addresses from the GAL (global address list)
Set-MailContact "$Contact.EmailAddress" -HiddenFromAddressListsEnabled $true -Whatif
}

Hope that helps in some way.

Thank You for the responses. I created a csv file with a single column “EmailAddresses” populated with the 580 addresses I need to import. However, I got the following error when running the script on the first and consecutive iterations… I stopped the script and copied the first part:

The external e-mail address @{EmailAddress=g84secretary@gmail.com}.EmailAddress is not an SMTP e-mail address.

  • CategoryInfo : NotSpecified: (EURPR04A002.pro…m}.EmailAddress:ADObjectId) [New-MailContact], Recipi
    entTaskException
  • FullyQualifiedErrorId : [Server=DB3PR0402MB3817,RequestId=71ec952f-a935-4273-b20a-5f907ca639fe,TimeStamp=05/08
    /2020 13:48:38] [FailureCategory=Cmdlet-RecipientTaskException] C86AE5F2,Microsoft.Exchange.Management.Recipient
    Tasks.NewMailContact
  • PSComputerName : outlook.office365.com

Couldn’t find object “@{EmailAddress=g84secretary@gmail.com}.EmailAddress”. Please make sure that it was spelled
correctly or specify a different object.

  • CategoryInfo : NotSpecified: (:slight_smile: [Add-DistributionGroupMember], ManagementObjectNotFoundException
  • FullyQualifiedErrorId : [Server=DB3PR0402MB3817,RequestId=9ddea277-eb5d-48a7-994f-18a2d10fd96f,TimeStamp=05/08
    /2020 13:48:39] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] F3967A20,Microsoft.Exchange.Managemen
    t.RecipientTasks.AddDistributionGroupMember
  • PSComputerName : outlook.office365.com

The operation couldn’t be performed because object ‘@{EmailAddress=g84secretary@gmail.com}.EmailAddress’ couldn’t be
found on ‘VI1PR04A002DC01.EURPR04A002.prod.outlook.com’.

  • CategoryInfo : NotSpecified: (:slight_smile: [Set-MailContact], ManagementObjectNotFoundException
  • FullyQualifiedErrorId : [Server=DB3PR0402MB3817,RequestId=f9a50a8f-999d-4d61-8bae-45dc93b899d7,TimeStamp=05/08
    /2020 13:48:41] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 3385B06A,Microsoft.Exchange.Managemen
    t.RecipientTasks.SetMailContact
  • PSComputerName : outlook.office365.com

The syntax "$Contact.EmailAddress" is not correct. It should be just $Contact.EmailAddress in your code.

Surrounding a PowerShell custom object in double quotes triggers string expansion. When that occurs, only the variable itself is substituted with its value and then the remaining characters not part of the name are treated as a string. You can see in the error that the custom object performed a ToString() and then just added the string .EmailAddress to it.

If you must have double quotes, then a way around it is to use the sub-expression operator $(). Anything inside is treated as an expression by the parser. So the more verbose “$($Contact.EmailAddress)”would technically work.

AdminOfThings45 is absolutely right. Apologies for the miss-type, I clearly wasn’t paying attention when I wrote my reply.

Hopefully that hasn’t been too confusing.