Forum Discussion

Deleted's avatar
Deleted
Jul 28, 2016

Help with Extracting list of contacts from specific domain

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  ...
  • Maor Bracha's avatar
    Maor Bracha
    Jul 28, 2016

    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) 

     

     

     

     

    $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;

     

  • Maor Bracha's avatar
    Maor Bracha
    Jul 29, 2016

    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.

     

     

     

    $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;

Resources