SOLVED
Home

Help with Extracting list of contacts from specific domain

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
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?

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.,

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

Hi,

 

Created directly on Exchange Online.

Thanks for the information. I'll whip somethig out with some explanations after I had my morning coffee and email round ; )
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;

 

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

 

 

Maor,

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

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

That is correct.

 

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

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;
Thanks for all the help Buddy....
My pleasure! Have a great weekend.

Maor