How to remove un-existed address from OWA autocomplete list

Introduction

This script is used to remove deleted mailboxes' address from all mailboxes' autocomplete list in OWA.

Scenarios

This script is used to remove deleted mailboxes' address from all mailboxes' autocomplete list in OWA, Recipient Cache and Suggested Contacts.

Script

You can use this script in the following way.

1. Open Windows PowerShell.

2. Type the one command< Script Path> at the Windows PowerShell Console.

For example, type C:\Script\ RemoveUnexistedAddressFromAutocomplete

Here are some code snippets for your references.

PowerShell
Edit|Remove
$StringData = [System.Text.Encoding]::UTF8.GetString($UsrConfig.XmlData).Substring(1)
 $xmlDoc = New-Object System.Xml.XmlDocument
 if($StringData -ne "")
 {
     $XmlDoc.LoadXml($StringData)
     $nodes = $xmlDoc.SelectNodes("/AutoCompleteCache/entry")
     foreach($node in $nodes)
     {
         if($node.smtpAddr -ne $null -and $node.smtpAddr.Contains($RemovedSMTPAddress))
         {
             Write-Host "contains"
         }
     }
         
     #Convert the xml back into a byte array
     $UpdatedData = [System.Text.Encoding]::UTF8.GetBytes([System.Text.Encoding]::UTF8.GetString($UsrConfig.XmlData).Substring(0,1) + $XmlDoc.OuterXml)
     $UsrConfig.XmlData = $UpdatedData
 
     #Save the config back to the users mailbox
     $UsrConfig.Update()

Examples

       Example 1: The address removeduser@contoso.com is no longer existed in organization. This script will remove this address from OWA's autocomplete in all mailboxes.

The sample is showed below
RemoveUnexistedAddressFromAutocomplete.ps1 -RemovedSMTPAddress removeduser@contoso.com

       Command Screenshot

      Result Screenshot

Before the script running

After the script running

Prerequisites

Exchange Online
Windows PowerShell 2.0

Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.