Share via

PowerShell script to loop through Exchange Online MailContacts in CSV file until they are deleted from Exchange Online

mark terry 185 Reputation points
2024-11-14T21:18:50.0466667+00:00

Hi all,

I have an input CSV file which contains the PrimarySMTPAddress values of Exchange Online MailContacts (like this):

PrimarySMTPAddress

******@test.com

******@test.com

These MailContacts are located in our On Premise Exchange Servers/Active Direcotry and are subsequently synced to Exchange Online.

Occasionally, we need to be able to delete these users from Exchange On Premise/Active Directory (and subsequently have those deletes sync to Exchange Online). I would like to have a PowerShell script so that when it is run, it will check to see when the MailContacts have been deleted from Exchange Online. The script requirements would be:

  1. Read the contents of the CSV file.
  2. Create a loop so that the script will check if the MailContacts have been deleted in Exchange Online.
  3. Once all users in the CSV file have been confirmed that they have been deleted from Exchange Online ID, exit the loop.

Thanks in advance!

Exchange Online
Exchange Online

A cloud-based service included in Microsoft 365, delivering scalable messaging and collaboration features with simplified management and automatic updates.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Anonymous
2024-11-15T08:31:16.9166667+00:00

Hi,@mark terry

Thanks for posting your question in the Microsoft Q&A forum.

First of all Exchange online forum doesn't support scripting, I hope you can understand that.

The following script is for reference only:

Please note that you will need the appropriate permissions to run this script and it assumes you have the Exchange Online PowerShell module installed and are connected to your Exchange Online session.

# Define the path to the CSV file

$csvFilePath = "C:\Users\Documents\test.csv"

$mailContacts = import-csv -path $csvFilePath  

foreach ($mailContact in $mailContacts) {  

# Try to get the MailContact from Exchange Online 

$onlineMailContact = Get-MailContact -Identity $mailContact.PrimarySMTPAddress -ErrorAction SilentlyContinue  

# Check if the MailContact exists in Exchange Online

 if ($onlineMailContact) {

 Write-Host "MailContact $($mailContact.PrimarySMTPAddress) still exists in Exchange Online."

   } else {

       Write-Host "MailContact $($mailContact.PrimarySMTPAddress) has been deleted from Exchange Online."

    }

 }

 Write-Host "All MailContacts have been checked."



If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.