SOLVED

Help with Extracting list of contacts from specific domain

%3CLINGO-SUB%20id%3D%22lingo-sub-3629%22%20slang%3D%22en-US%22%3EHelp%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3629%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20to%20extarct%20list%20of%20Contacts%20lets%20say%20abc.com%20from%20various%20DL's%20with%20in%20ORG.%20Once%20the%20list%20is%20extracted%20then%20modify%20the%20smtp%20address%20for%20these%20contacts%20from%20abc.com%20to%20xyz.com%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20some%20one%20let%20me%20know%20how%20best%20can%20this%20be%20achieved%3F%20or%20any%20one%20has%20any%20script%20already%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3629%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3756%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3756%22%20slang%3D%22en-US%22%3EMy%20pleasure!%20Have%20a%20great%20weekend.%3CBR%20%2F%3E%3CBR%20%2F%3EMaor%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3755%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3755%22%20slang%3D%22en-US%22%3EThanks%20for%20all%20the%20help%20Buddy....%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3753%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3753%22%20slang%3D%22en-US%22%3E%3CP%3EHere%20you%20go.%3C%2FP%3E%3CP%3ESame%20idea%20-%20save%20as%20.ps1%20or%20download%20the%20attached%20file.%20Run%20from%20Exchange%20online%20PowerShell%20%26gt%3B%20input%20domain%20to%20search%20and%20wait%20for%20results.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%3E%3CIMG%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22printscreen.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F1426i06BEB585BCC2D5D3%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22printscreen.png%22%20alt%3D%22printscreen.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24Domain%20%3D%20%22*%22%2B(Read-Host%20-Prompt%20%22Please%20input%20domain%20to%20search%20for%22)%3B%0A%24Groups%20%3D%20Get-DistributionGroup%20-ResultSize%20Unlimited%3B%0AForeach(%24Group%20in%20%24Groups)%0A%20%7B%0A%20%24MembersWithDomain%20%3D%20Get-DistributionGroupMember%20%24Group.Alias%20%7C%20Where%20%7B(%24_.PrimarySmtpAddress.toLower()%20-like%20%24Domain.toLower())%20-and%20(%24_.RecipientType%20-eq%20%22MailContact%22)%7D%3B%0A%20If(%24MembersWithDomain)%7B%24GroupsWithDomain%2B%3D%40(%24Group)%3B%24Count%2B%3D1%7D%0A%20%7D%0AWrite-Host%20%22Found%20%22%24Count%22%20groups%20containing%20contacts%20with%20a%20primary%20address%20matching%20the%20domain%22%3B%0AWrite-Host%20%22Printing%20list%20to%20screen%20in%203%20seconds%22%3B%0AStart-Sleep%20-S%203%3B%0A%24GroupsWithDomain%3B%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3749%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3749%22%20slang%3D%22en-US%22%3E%3CP%3EThat%20is%20correct.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Equery%20to%20see%20how%20many%20DL's%20are%20showing%20these%20contacts%20as%20members%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3748%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3748%22%20slang%3D%22en-US%22%3EIf%20I%20understand%20correctly%2C%20you%20want%20to%20go%20over%20all%20your%20DLs%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3747%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3747%22%20slang%3D%22en-US%22%3EMaor%2C%3CBR%20%2F%3E%3CBR%20%2F%3EHow%20do%20i%20modify%20this%20script%20to%20query%20the%20concerned%20Contacts%20membership%20from%20various%20DL's%3F%3CBR%20%2F%3E%3CBR%20%2F%3EThanks%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3746%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3746%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%20so%20much%20for%20taking%20out%20your%20time%20and%20helping%20me.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3670%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3670%22%20slang%3D%22en-US%22%3E%3CP%3ETook%20a%20bit%20longer%20than%20expected%2C%20had%20to%20finish%20some%20obligations.%26nbsp%3BAnyway%2C%20here%20goes%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESave%20the%20following%20code%20block%20as%20.ps1%20(or%20download%20attachment)%20and%20run%20from%20Exchange%20online%20PoweShell.%3C%2FP%3E%3CP%3EIt%20will%20ask%20you%20for%20the%20old%20domain%2C%20new%20domain%2C%20DL%20name%20and%20an%20output%20path%20for%20a%20log%20file.%3C%2FP%3E%3CP%3EA%20log%20will%20also%20be%20printed%20to%20your%20screen%20for%20your%20convenience%2C%20letting%20you%20know%20which%20contacts%20were%20modified%20and%20which%20were%20not%20since%20the%20address%20don't%20match%20(see%20print%20screen)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20notice%20that%20this%20script%20works%20on%20%3CEM%3E%3CU%3Email-contacts%3C%2FU%3E%3C%2FEM%3E%20as%20requested%2C%20so%20if%20you%20have%20mailboxes%5Cmail-users%20-%20they%20will%20bot%20be%20modified.%20But%20that%20should%20be%20easy%20for%20you%20to%20change%20in%20the%20script%20(just%20change%20the%20%22Set-MailContact%22%20line%20to%20%22Set-Mailbox%22%20or%20%22Set-MailUser%22%20if%20required)%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Namnl%C3%B6s.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F1417i8DC1F72F8A649D5E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Namnl%C3%B6s.png%22%20alt%3D%22Namnl%C3%B6s.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24DL%20%3D%20Read-Host%20-Prompt%20%22Please%20Input%20DL%20Alias%22%3B%0A%24OldDomain%20%3D%20%22*%22%2B(Read-Host%20-Prompt%20%22Please%20Input%20domain%20to%20be%20converted%22)%3B%0A%24NewDomain%20%3D%20Read-Host%20-Prompt%20%22Please%20Input%20designated%20new%20domain%22%3B%0A%24OutPath%20%3D%20Read-Host%20-Prompt%20%22Please%20input%20a%20full%20log%20output%20path%2C%20including%20file%20name%20with%20'.txt'%20ending%22%3B%0A%24DLMembers%20%3D%20Get-DistributionGroupMember%20%24DL%3B%0AForeach(%24Member%20in%20%24DLMembers)%0A%7B%0A%20If(%24Member.PrimarySmtpAddress.toLower()%20-like%20%24OldDomain.toLower())%0A%20%7B%0A%20%24NewAddress%20%3D%20%24Member.PrimarySmtpAddress.toLower().Replace(%24OldDomain.toLower().trim(%22*%22)%2C%24NewDomain)%3B%0A%20Set-MailContact%20%24Member.Alias%20-WindowsEmailAddress%20%24NewAddress%3B%0A%20%24Line%20%3D%20%22Found%20mail-contact%20%22%2B%24Member.Alias%2B%22%20%2C%20Replaced%20address%20%22%2B%24Member.PrimarySmtpAddress%2B%22%20with%20the%20address%20%24NewAddress%22%3B%0A%20%24Output%20%2B%3D%20%40(%24Line)%3B%0A%20%24Line%3B%0A%20%7D%0A%20If(!(%24Member.PrimarySmtpAddress.toLower()%20-like%20%24OldDomain.toLower()))%7B%0A%20%24Line%20%3D%20%24Line%20%3D%20%22Found%20mail-contact%20%22%2B%24Member.Alias%2B%22%20%2C%20but%20no%20changes%20made%20since%20primary%20address%20does%20not%20match%20domain%20to%20be%20converted%22%3B%0A%20%24Output%20%2B%3D%20%40(%24Line)%3B%0A%20%24Line%3B%0A%20%7D%0A%0A%7D%0A%24Output%7COut-File%20%24OutPath%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3637%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3637%22%20slang%3D%22en-US%22%3ESure.%20Thanks%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3635%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3635%22%20slang%3D%22en-US%22%3EThanks%20for%20the%20information.%20I'll%20whip%20somethig%20out%20with%20some%20explanations%20after%20I%20had%20my%20morning%20coffee%20and%20email%20round%20%3B%20)%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3633%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3633%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECreated%20directly%20on%20Exchange%20Online.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3632%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3632%22%20slang%3D%22en-US%22%3EGreat.%20Just%20to%20make%20sure-%20are%20those%20contacts%2FDLs%20synced%20from%20AD%20or%20created%20directly%20on%20Office%20365%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3631%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3631%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Maor%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E1.%20Is%20it%20primary%20addresses%20you%20would%20like%20to%20change%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EAnswer%3A%20Yes%26nbsp%3B%3CBR%20%2F%3E%3CSPAN%3E2.%20Would%20you%20like%20to%20keep%20the%20old%20ones%20as%20secondary%20addresse%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EAnswer%20is%20No%3CBR%20%2F%3E%3CSPAN%3E3.%20Is%20it%20an%20Exchange%20on-prem%2C%20Exchange%20online%20or%20hybrid%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EAnswer%3A%20It%20is%20Exchange%20Online.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThanks%20for%20asking.%2C%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3630%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20Extracting%20list%20of%20contacts%20from%20specific%20domain%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3630%22%20slang%3D%22en-US%22%3EI%20can%20help%20you%20out%20with%20this%20script%2C%20but%20I'll%20need%20to%20know%3A%3CBR%20%2F%3E1.%20Is%20it%20primary%20addresses%20you%20would%20like%20to%20change%3F%3CBR%20%2F%3E2.%20Would%20you%20like%20to%20keep%20the%20old%20ones%20as%20secondary%20addresse%3F%3CBR%20%2F%3E3.%20Is%20it%20an%20Exchange%20on-prem%2C%20Exchange%20online%20or%20hybrid%3F%3C%2FLINGO-BODY%3E
Highlighted
Deleted
Not applicable

Hello All,

 

I need to extarct list of Contacts lets say abc.com from various DL's with in ORG. Once the list is extracted then modify the smtp address for these contacts from abc.com to xyz.com

 

Can some one let me know how best can this be achieved? or any one has any script already?

 

Thanks

14 Replies
Highlighted
I can help you out with this script, but I'll need to know:
1. Is it primary addresses you would like to change?
2. Would you like to keep the old ones as secondary addresse?
3. Is it an Exchange on-prem, Exchange online or hybrid?
Highlighted

Hello Maor,

 

1. Is it primary addresses you would like to change?

Answer: Yes 
2. Would you like to keep the old ones as secondary addresse?

Answer is No
3. Is it an Exchange on-prem, Exchange online or hybrid?

Answer: It is Exchange Online.

 

Thanks for asking.,

Highlighted
Great. Just to make sure- are those contacts/DLs synced from AD or created directly on Office 365?
Highlighted

Hi,

 

Created directly on Exchange Online.

Highlighted
Thanks for the information. I'll whip somethig out with some explanations after I had my morning coffee and email round ; )
Highlighted
Highlighted
Best Response
Solution

Took a bit longer than expected, had to finish some obligations. Anyway, here goes:

 

Save the following code block as .ps1 (or download attachment) and run from Exchange online PoweShell.

It will ask you for the old domain, new domain, DL name and an output path for a log file.

A log will also be printed to your screen for your convenience, letting you know which contacts were modified and which were not since the address don't match (see print screen)

 

Please notice that this script works on mail-contacts as requested, so if you have mailboxes\mail-users - they will bot be modified. But that should be easy for you to change in the script (just change the "Set-MailContact" line to "Set-Mailbox" or "Set-MailUser" if required) 

 

 

Namnlös.png

 

 

$DL = Read-Host -Prompt "Please Input DL Alias";
$OldDomain = "*"+(Read-Host -Prompt "Please Input domain to be converted");
$NewDomain = Read-Host -Prompt "Please Input designated new domain";
$OutPath = Read-Host -Prompt "Please input a full log output path, including file name with '.txt' ending";
$DLMembers = Get-DistributionGroupMember $DL;
Foreach($Member in $DLMembers)
{
	If($Member.PrimarySmtpAddress.toLower() -like $OldDomain.toLower())
	{
	$NewAddress = $Member.PrimarySmtpAddress.toLower().Replace($OldDomain.toLower().trim("*"),$NewDomain);
	Set-MailContact $Member.Alias -WindowsEmailAddress $NewAddress;
	$Line = "Found mail-contact "+$Member.Alias+" , Replaced address "+$Member.PrimarySmtpAddress+" with the address $NewAddress";
	$Output += @($Line);
	$Line;
	}
	If(!($Member.PrimarySmtpAddress.toLower() -like $OldDomain.toLower())){
	$Line = $Line = "Found mail-contact "+$Member.Alias+" , but no changes made since primary address does not match domain to be converted";
	$Output += @($Line);
	$Line;
	}

}
$Output|Out-File $OutPath;

 

Highlighted

Thank you so much for taking out your time and helping me.

 

 

Highlighted
Maor,

How do i modify this script to query the concerned Contacts membership from various DL's?

Thanks
Highlighted
If I understand correctly, you want to go over all your DLs?
Highlighted

That is correct.

 

query to see how many DL's are showing these contacts as members?

Highlighted
Best Response
Solution

Here you go.

Same idea - save as .ps1 or download the attached file. Run from Exchange online PowerShell > input domain to search and wait for results.

 

printscreen.png

 

 

$Domain = "*"+(Read-Host -Prompt "Please input domain to search for");
$Groups = Get-DistributionGroup -ResultSize Unlimited;
Foreach($Group in $Groups)
	{
	$MembersWithDomain = Get-DistributionGroupMember $Group.Alias | Where {($_.PrimarySmtpAddress.toLower() -like $Domain.toLower()) -and ($_.RecipientType -eq "MailContact")};
	If($MembersWithDomain){$GroupsWithDomain+=@($Group);$Count+=1}
	}
Write-Host "Found "$Count" groups containing contacts with a primary address matching the domain";
Write-Host "Printing list to screen in 3 seconds";
Start-Sleep -S 3;
$GroupsWithDomain;
Highlighted
Thanks for all the help Buddy....
Highlighted
My pleasure! Have a great weekend.

Maor