Import  emailaddresses, exported from a distribution Group, to another DG

Ernesto Gallardo
Ernesto Gallardo used Ask the Experts™
on
Hi ,
I have a csv file with with the email addresses used by a DG. NOT the email address from the members of the DG , I'm referring to the smtp addresses used by the DG. For Instance: if the DG name is "Graduated students" with email addresses like:  gradstudents@domain.com , gradstudents@domain.onmicrosoft.com, gradstudents@mail.domain.com...
I need a sentence to import those email addresses into another DG in O365.
Thanks,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Subash SundharanIT Infrastructure Architect

Commented:
For single group you can use following code..
Set-DistributionGroup testgroup -emailaddresses @{Add='gradstudents@domain.com','gradstudents@domain.onmicrosoft.com','gradstudents@mail.domain.com'}

Open in new window

With csv file...
Import-csv C:\input.csv | %{Set-DistributionGroup $_.GroupName -emailaddresses @{Add=($_.Email -Split ",")}}

Open in new window

Sample CSV
GroupName,Email
"Graduated students","gradstudents@domain.com,gradstudents@domain.onmicrosoft.com,gradstudents@mail.domain.com"

Open in new window

Emails will be updated to group 'Graduated students'

In case you if you don't have the export command..
Get-DistributionGroup "Graduated students" | Select-Object DisplayName,PrimarySmtpAddress,@{N=“Email”;E={($_.EmailAddresses |? {$_.PrefixString -ceq “smtp”} | % {$_.SmtpAddress}) -Join ","}} | Export-csv C:\group.csv -nti

Open in new window

Author

Commented:
Thanks Again, I think we are close but , let me explain it more.
Since I'm performing a migration  of all the DGs managed by the exchange on premises to O365, I'm running a script that pre-creates the DG in O365 with the prefix "Cloud-" followed by the name of the Group to migrate. but this new pre-created group can't have the email addresses of the old one since they are still assigned to that old group.
So, I'm doing

Import-CSV C:\input.csv | ForEach {Get-DistributionGroup -Identity $_.name | select Displayname, emailaddresses} | Export-Csv C:\emailaddresses1.csv -NoTypeInformation
 
This will give me a csv file with 2 columms :
Column A= GroupName (e.g: ACE Portfolio)
Column B= Emailaddresses (e.g:  smtp:ace_port@mail.barry.edu SMTP:ace_port@barry.edu smtp:ace_port@barryuniversity.onmicrosoft.com x500:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=7bba86b5543b4192a1d12a45115c7d91-ACE Portfolio X400:C=US;A= ;P=BARRYU;O=BUMAIN;S=ace?port; smtp:ace_port@barrynet.barry.edu)

After that I delete the groups from the exchange on premises and AD and  then After the DGs are also deleted from O365, then I should import the email addresses and remove the cloud- from the name of the group.
When running your sentence adjusted to my  names, etc.. this is the error I'm getting

"Cannot process argument transformation on parameter 'EmailAddresses'. Cannot
convert value "System.Collections.Hashtable" to type
"Microsoft.Exchange.Data.ProxyAddressCollection". Error: "MultiValuedProperty
collections cannot contain null values.
Parameter name: item"
    + CategoryInfo          : InvalidData: (:) [Set-DistributionGroup], Parame
   terBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Distrib
   utionGroup
    + PSComputerName        : ps.outlook.com"

Looks like it can't read it in the format i have it...
Subash SundharanIT Infrastructure Architect
Commented:
Try the following command to export..
Import-CSV C:\input.csv | ForEach {Get-DistributionGroup -Identity $_.name | select Displayname,@{N=“Email”;E={($_.EmailAddresses |? {$_.PrefixString -eq “smtp”} | % {$_.SmtpAddress}) -Join ","}}} | Export-Csv C:\emailaddresses1.csv -NoTypeInformation

Open in new window

To import..
Import-csv C:\input.csv | %{Set-DistributionGroup $_.GroupName -emailaddresses @{Add=($_.Email -Split ",")}}

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Thanks Subsun, but the file exported has empty the column for Email. Only the DisplayName is populated with the Groups.
Subash SundharanIT Infrastructure Architect

Commented:
Just tested, It's working for me.. Does the group shows result for following command
Get-DistributionGroup TestGroup | select Displayname,@{N=“Email”;E={($_.EmailAddresses |? {$_.PrefixString -eq “smtp”} | % {$_.SmtpAddress}) -Join ","}}

Open in new window

If above fails, please post the result for..
Get-DistributionGroup TestGroup | select -exp EmailAddresses

Open in new window

Author

Commented:
Hi Subsun,
I had to work on something else. I'll test this tonight and I'll let you know tomorrow. Thanks for all your help . I really appreciate it.

Author

Commented:
The first one failed. It doesn't shows anything on the email part. Just empty.
The second one Works this way:

PS C:\Users\XXXXXX> Get-DistributionGroup "ACE Orlando" | select -exp EmailAddresses
SMTP:ace_orlando@barry.edu
smtp:ace_orlando@mail.barry.edu
smtp:ace_orlando@barryuniversity.onmicrosoft.com
x500:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=76dcb5395beb4525b89e83e450035a
82-ACE Orlando
X400:C=US;A= ;P=BARRYU;O=BUMAIN;S=ace?orlando;
smtp:ace_orlando@barrynet.barry.edu
Subash SundharanIT Infrastructure Architect

Commented:
Is that the exact result you get in powershell console?

Can you test this command and see result?
Get-DistributionGroup TestGroup | select Displayname,@{N=“Email”;E={($_.EmailAddresses |? {$_ -match “smtp”} | % {($_ -split ":")[1]}) -Join ","}}

Open in new window

Author

Commented:
DisplayName                                                 Email
 ACE Orlando                                             ace_orlando@barry.edu,ace_orlando@mail.barry.edu,ace_orl...
 This one looks good  with emails separated by coma..
Thank you Subsun
Subash SundharanIT Infrastructure Architect

Commented:
Ok you can use the same to export the list..
Import-CSV C:\input.csv | ForEach {Get-DistributionGroup -Identity $_.name | select Displayname,@{N=“Email”;E={($_.EmailAddresses |? {$_ -match “smtp”} | % {($_ -split ":")[1]}) -Join ","}} | Export-Csv C:\emailaddresses1.csv -NoTypeInformation

Open in new window

Author

Commented:
Looks like it should work but is stays like this:

PS C:\Users\gallo> Import-CSV S:\egallardo\test.csv | ForEach {Get-DistributionGroup -Identity $_.name | select Displayname,@{N="Email";E={($_.EmailAddresses |? {$_ -match "smtp"} | % {($_ -split ":")[1]}) -Join ","}} | Export-Csv S:\egallardo\Migration\emailaddresses2.csv -NoTypeInformation
>>
>>
And file emailaddresses2.csv is not created.
 I even reduce the amount of DGs in test.csv   to 19 and still....nothing.it wont come out of that state...
 I had to ctrl+C  to get out of that state.
IT Infrastructure Architect
Commented:
Ah syntax error.. Missed the closing curly brace for ForEach ..
Import-CSV C:\input.csv | ForEach {Get-DistributionGroup -Identity $_.name | select Displayname,@{N=“Email”;E={($_.EmailAddresses |? {$_ -match “smtp”} | % {($_ -split ":")[1]}) -Join ","}}} | Export-Csv C:\emailaddresses1.csv -NoTypeInformation

Open in new window

Author

Commented:
Yes, That was it , perfect , Now it is created with that format , separated by coma only .
So to later import them into the pre-created ones in O365:
1- Delete from O365 those DGs that I already have pre-created them in O365 and also their email addresses exported.
2.-Remove the Cloud- prefix to that list of pre-created DGs
3.- Now I'm doing : Import-csv S:\Migration\emailaddresses2.csv | %{Set-DistributionGroup -Identity $_.Displayname -emailaddresses @{Add=($_.Email -Split ",")}}       (As you suggested)  And Is working Just Fine.
I just need to find a better way now to remove the Cloud- and to reassign the correct email address as the one to use to reply. But I'll try to find it by my self, So far you have Helped me A LOT!!!
Thank you So much  Subsun
Subash SundharanIT Infrastructure Architect

Commented:
You are welcome!..

Author

Commented:
Susbsun,is the Best, Always replied quickly, and he was helping me all the way until the correct sentence was created. And all that at any time, You guys rock!!!!
Subash SundharanIT Infrastructure Architect

Commented:
Happy to help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial