Clearing AutoComplete and other Recipient Caches

Exchange 2010 Logo

Last version: 1.21, April 28th, 2021: Updated formatting and link to GitHub

Anyone who has participated in migrations or transitions to Exchange has most likely encountered or has had to work around potential issues caused by the nickname cache. A “cache,” also known by its file extension, NK2 in older Outlook clients, is a convenience feature in Outlook and Outlook WebApp (OWA) which lets users pick recipients from a list of frequently-used recipients. This list is displayed when the end user types in the first few letters.

The potential issue revolves around end users using those lists to send messages, as the list contains cached recipient information. Because this information is static, it may become invalid at some point. Thus, when users pick recipients when sending messages, they may be sending messages to non-existent recipients or invalid e-mail addresses, which create issues like non-delivery of e-mail.

Read the full article over on ENow Solutions Engine blog.

Clean-AutoComplete

Using the script mentioned in the article, which can be used to clear cached recipient information, is straightforward. It requires Exchange 2010 or later and Exchange Web Services Managed API 1.2 (or later) which you can download here. Alternatively, you can copy the Microsoft.Exchange.WebServices.DLL with the script as it will also look for it in the current folder.

The script Clean-AutoComplete.ps1 has the following syntax:

Clear-AutoComplete.ps1 [-Mailbox] <String> [-Server <String>] [-Impersonation] [-Credentials <PSCredential>] [-Type <Array>] [-Pattern <String[]>]

Where:

  • Mailbox is the name or e-mail address of the mailbox.
  • Server is the name of the Client Access Server to access for Exchange Web Services. When omitted, the script will use AutoDiscover.
  • Switch Impersonation specifies if impersonation will be used for mailbox access, otherwise the current user context will be used.
  • Credentials specifies the user credentials to use.
  • Type specifies what cached recipient information to clear. Options are Outlook  (Outlook AutoComplete stream), OWA (OWA Autocomplete stream), SuggestedContacts, RecipientCache or All. Default is Outlook,OWA.
  • Pattern is the pattern of e-mail entries to remove from cache. Only works with OWA, SuggestedContacts and RecipientCache type clearances.

So for example, suppose you want to clear the Autocomplete stream used by Outlook on a mailbox, you can use:

Clear-AutoComplete.ps1 -Identity Olrik -Type Outlook -Verbose
ScreenCap

To remove the Autocomplete stream used by OWA on your Office 365 account, you can use:

Clear-AutoComplete.ps1 -Identity olrik@office365tenant.com –Credentials (Get-Credential) –Type OWA

Be advised that clearing the Outlook AutoComplete stream will only have effect for Outlook running in Online mode. Outlook caches this information as well in the OST file, leaving the options of running Outlook with the /CleanAutocompleteCache switch, or remove and let Outlook recreate the OST file. The temporary Stream_AutoComplete *.dat files created under %USERPROFILE%\AppData\Local\Microsoft\Outlook\RoamCache are used by Outlook to speed things up.

Disabling Auto-Complete and Suggested Contacts
Alternatively, you can disable Auto-Complete, the equivalent of unchecking the Outlook option ‘Use Auto-Complete List to suggest names when typing in the To, Cc and Bcc line‘, by setting the following registry key:

Note: In the examples below, you need to modify the version number in the examples corresponding to the Outlook version you wish to apply these settings against. Use 16.0 as indicated for Outlook 2016, but change it to 15.0 for Outlook 2013, or 14.0 for Outlook 2010.

HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Preferences\
ShowAutoSug=0 (REG_DWORD)

To configure this setting using a Group Policy, use the following registry setting:

HKEY_CURRENT_USER\Software\Policies\Microsoft\office\16.0\Outlook\Preferences\ShowAutoSug=0 (REG_DWORD)

You can also disable Suggested Contacts folder, the equivalent of unchecking the Outlook option ‘Automatically create Outlook contacts for recipients that do not belong to an Outlook Address Book’, with the following registry key:

HKEY_CURRENT_USER\Software\Microsoft\office\16.0\Outlook\Contact\CreateContactsForOneOffs= 0 (REG_DWORD)

The related Group Policy setting is:

HKEY_CURRENT_USER\Software\Policies\Microsoft\office\16.0\Outlook\Contact\CreateContactsForOneOffs= 0 (REG_DWORD)

Feedback
Feedback is welcomed through the comments. If you got scripting suggestions or questions, do not hesitate using the contact form.

Download
You can download the script from GitHub here.

62 thoughts on “Clearing AutoComplete and other Recipient Caches

  1. Pingback: Weekly IT Newsletter – July 14-18, 2014 | Just a Lync Guy

  2. Pingback: NeWay Technologies – Weekly Newsletter #104 – July 17, 2014 | NeWay

  3. Pingback: NeWay Technologies – Weekly Newsletter #104 – July 18, 2014 | NeWay

  4. I’m receiving the following error message. Any suggestions?

    PS C:\Office365\PowerShell> .\Clear-AutoComplete.ps1 -mailbox xxx@domain -Server outlook.office365.com -type All -Verbose
    VERBOSE: Loading C:\Program Files\Microsoft\Exchange\Web Services\2.0\\Microsoft.Exchange.WebServices.dll
    VERBOSE: Loaded Microsoft.Exchange.WebServices v15.0.0.0
    Processing mailbox xxx@domain
    VERBOSE: Set to trust all certificates
    VERBOSE: Using Exchange Web Services URL https://outlook.office365.com/EWS/Exchange.asmx
    C:\Office365\PowerShell\Clear-AutoComplete.ps1 : Can’t access mailbox information store
    At line:1 char:1
    + .\Clear-AutoComplete.ps1 -mailbox xxx@domain -Server outlook.office3 …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Clear-AutoComplete.ps1

    Also tried the following:

    PS C:\Office365\PowerShell> .\Clear-AutoComplete.ps1 -mailbox xxx@domain -type All -Verbose
    VERBOSE: Loading C:\Program Files\Microsoft\Exchange\Web Services\2.0\\Microsoft.Exchange.WebServices.dll
    VERBOSE: Loaded Microsoft.Exchange.WebServices v15.0.0.0
    Processing mailbox xxx@domain
    VERBOSE: Set to trust all certificates
    VERBOSE: Looking up EWS URL using Autodiscover for xxx@domain
    C:\Office365\PowerShell\Clear-AutoComplete.ps1 : A positional parameter cannot be found that accepts argument ‘Exception calling “AutodiscoverUrl” with “2” argument(s): “The Autodiscover service couldn’t be located.”‘.
    At line:1 char:1
    + .\Clear-AutoComplete.ps1 -mailbox xxx@domain -type All -Verbose
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Clear-AutoComplete.ps1], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Clear-AutoComplete.ps1

    Like

    • 1) “Can’t access mailbox information store”.
      Seems like permission issue, see https://eightwone.com/2014/08/13/application-impersonation-to-be-or-pretend-to-be/

      2) “The Autodiscover service couldn’t be located”
      The Autodiscover service is not configured for @domain or there is a problem accessing it or a problem with Autodiscover.

      Are you running this for an Office 365 mailbox? Are you running this for on-prem for an on-prem mailbox?

      Like

      • I get a similar “AutodiscoverUrl” error.

        VERBOSE: Loading C:\Program Files\Microsoft\Exchange\Web Services\2.2\\Microsoft.Exchange.WebServices.dll
        VERBOSE: Loaded Microsoft.Exchange.WebServices v15.0.0.0
        Processing mailbox user123@xyz.onmicrosoft.com (user123@xyz.onmicrosoft.com)
        VERBOSE: Set to trust all certificates
        VERBOSE: Using credentials user123
        VERBOSE: Looking up EWS URL using Autodiscover for user123@xyz.onmicrosoft.com
        C:\Users\user123\Clear-AutoComplete.ps1 : A positional parameter cannot be found that accepts argument ‘Exception
        calling “AutodiscoverUrl” with “2” argument(s): “The Autodiscover service couldn’t be located.”‘.
        At line:1 char:1
        + .\clear-autocomplete.ps1 -Mailbox user123@xyz.onmicrosoft.com
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : InvalidArgument: (:) [Clear-AutoComplete.ps1], ParameterBindingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,Clear-AutoComplete.ps1

        What is the Autodiscover service, and do I “need” it for this script/process to run/work?

        Thanks,
        Dave

        Like

  5. Hi Michel,

    I’m working on a Exchange2013 On-Premise installation and got your script run with a few changes:

    – line 240: $Folder.Empty() needs two parameters. Second one could be $true/$false for the recursive folder deletion. It’s mandatory

    – line 260: $RecipientCacheFolder.Empty() needs two parameters. Second one could be $true/$false for the recursive folder deletion. It’s mandatory

    – line 287: [Microsoft.Exchange.WebServices.Data.ExchangeVersion] changed from “Exchange2010_SP1” to “Exchange2013”

    I also had to run the following cmdlet to let the impersonation work as expected:

    New-ManagementRoleAssignment –Name:impersonationAssignmentName Role:ApplicationImpersonation –User:()

    Regards,

    Simone

    Like

    • Thanks for the feedback. Note that you can use Exchange2010 ‘mode’ to communicate against Exchange 2013+, it’s only to unlock Exchange2013+ specific operations (and syntax as you found out 🙂 ).

      Like

  6. Pingback: Something More about Autocomplete | nehrunotes

  7. Hi, I get the following error:

    C:\Scripts\Migration\LMH\Clear-AutoComplete.ps1 : Can’t access mailbox information store
    At line:1 char:25
    + .\Clear-AutoComplete.ps1 <<<< -Mailbox tabcd -Credentials $cred -Type All -Verbose -Server owa.ynhh.org
    + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Clear-AutoComplete.ps1

    I'm using an account that has full access to all mailboxes. Should I use the Impersonation option instead?

    Like

    • Here is the verbose information:

      [PS] C:\Scripts\Migration\LMH>.\Clear-AutoComplete.ps1 -Mailbox tabcd -Credentials $cred -Type All -Verbose -Server NLBNAME
      VERBOSE: Loading C:\Scripts\Migration\LMH\Microsoft.Exchange.WebServices.dll
      VERBOSE: Loaded Microsoft.Exchange.WebServices v14.0.0.0
      Processing mailbox tabcd (Test.ABCD@domain.com)
      VERBOSE: Set to trust all certificates
      VERBOSE: Using credentials account info
      VERBOSE: Using Exchange Web Services URL https://NLBNAME/EWS/Exchange.asmx
      C:\Scripts\Migration\LMH\Clear-AutoComplete.ps1 : Can’t access mailbox information store
      At line:1 char:25
      + .\Clear-AutoComplete.ps1 <<<< -Mailbox tabcd -Credentials $cred -Type All -Verbose -Server owa.ynhh.org
      + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Clear-AutoComplete.ps1

      Like

    • Does the account have proper full permissions configured, and you took into account a period to wait for FMA to become effective?
      Configure impersonation and try that route; if that works, there was perhaps something wrong with (in-mailbox) permissions.
      If it doesn’t, is there perhaps a firewall blocking port 443 traffic on that port?

      Like

      • Hi Michael, Thanks for the quick response. This is on-prem, so the firewall blocking should not come into play, correct?

        I just tired again and if failed again.

        My account does have impersonation rights:
        MAImpersonationAssignmentNa… ApplicationImpersonation Boluk, Sahin User Direct Boluk, Sahin

        Like

              • Hi Michael,
                I figured out how to run the script. I was using domain\ID, and I tried UserID@domain and that works now.

                Only thing is, it runs but doesn’t clear the cache. I tested with a test account, and below is the output, says no item found, but there is on the mailbox, any thoughts?

                VERBOSE: Loading C:\Scripts\Migration\LMH\Microsoft.Exchange.WebServices.dll
                VERBOSE: Loading module from path ‘C:\Scripts\Migration\LMH\Microsoft.Exchange.WebServices.dll’.
                VERBOSE: Loaded Microsoft.Exchange.WebServices v14.0.0.0
                Processing mailbox xxxxxx
                VERBOSE: Set to trust all certificates
                VERBOSE: Using credentials xxxxxx
                VERBOSE: Using Exchange Web Services URL https://xxxxxx/EWS/Exchange.asmx
                VERBOSE: No AutoComplete Stream item found.
                VERBOSE: No OWA AutoComplete item found.
                VERBOSE: No ‘Suggested Contacts’ folder found.
                VERBOSE: No RecipientCache folder found.

                Like

  8. Hello Michel! Thank you for the article and the script, greatly appreciate it. Recently we had someone setup a user incorrectly, which we then had to re-create with the proper name. After which, some of our users can email correctly, but others cannot email. We seem to get a NDR, mainly when using the Auto-Complete. I stumbled across your article and figured I’d give it a try but when running I am seeing the following:

    New-Object : Multiple ambiguous overloads found for “ExchangeService” and the argument count: “1”.
    At C:\temp\Clear-AutoComplete.ps1:291 char:22
    + $EwsService= New-Object Microsoft.Exchange.WebServices.Data.ExchangeServ …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    The property ‘UseDefaultCredentials’ cannot be found on this object. Verify that the property exists and can be set.
    At C:\temp\Clear-AutoComplete.ps1:292 char:9
    + $EwsService.UseDefaultCredentials= $true
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

    The property ‘UseDefaultCredentials’ cannot be found on this object. Verify that the property exists and can be set.
    At C:\temp\Clear-AutoComplete.ps1:305 char:13
    + $EwsService.UseDefaultCredentials= $true
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

    VERBOSE: Looking up EWS URL using Autodiscover for {MAILBOX NAME}
    C:\temp\Clear-AutoComplete.ps1 : Autodiscover failed: You cannot call a method on a null-valued expression.
    At line:1 char:1
    + .\Clear-AutoComplete.ps1 -Mailbox {MAILBOX NAME} -Type Outlook -Verbose
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Clear-AutoComplete.ps1

    Any thoughts as to what would be causing this? I am running the Exchange Management PS as a domain admin account, on the Exchange server and have ensured the API is installed as well.

    Thanks in advance,
    Kyle

    Like

  9. Hi.
    I am a complete newbie for Powershell but the auto suggestion have annoying me off and I decided to see if I could run your script.

    I get the following message when running it in powershell (With admin)

    PS C:\test> .\Clear-AutoComplete.ps1 -mailbox thomas@XXX.XX -Server outlook.office365.com -type All -Verbose
    C:\test\Clear-AutoComplete.ps1 : A parameter cannot be found that matches parameter name ‘mailbox’.
    At line:1 char:26
    + .\Clear-AutoComplete.ps1 -mailbox thomas@t-and.dk -Server outlook.off …
    + ~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Clear-AutoComplete.ps1], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Clear-AutoComplete.ps1
    What am I doing wrong ?

    Like

  10. Hi Michel,
    Thank you for writing the script.
    I was able to clear run the script and able to clear the Outlook Autocomplete.
    However, get message “No OWA AutoComplete item found” for OWA.
    Please share your thoughts.

    Like

      • So, can’t find the user to whom the script is directed?
        Also during the execution of the script sometimes I get an error.

        .\Clear-AutoComplete.ps1 -Identity username@tenant.com -Credentials (Get-Credential) -Type OWA

        Processing mailbox username@tenant.com

        Write-Verbose : A parameter cannot be found that matches parameter name “f”.
        C:\ps_test\Clear-AutoComplete.ps1:464 char:50
        + Write-Verbose ‘Using EWS on CAS {0}’ -f $EwsService.Url
        + ~~
        + CategoryInfo : InvalidArgument: (:) [Write-Verbose], ParameterBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.WriteVerboseCommand

        Processing OWA AutoComplete for username@tenant.com
        No OWA AutoComplete item found

        Like

  11. Hey Michel,

    when i am running the script in verbose mode, I can the following output:
    AUSFÜHRLICH: Loading C:\Program Files\Microsoft\Exchange\Web Services\2.2\\Microsoft.Exchange.WebServices.dll
    AUSFÜHRLICH: Loaded Microsoft.Exchange.WebServices v15.0.0.0
    Processing mailbox HIDDEN (HIDDEN)
    AUSFÜHRLICH: Set to trust all certificates
    AUSFÜHRLICH: Using credentials gh-adressbuch@die-frischemacher.de
    AUSFÜHRLICH: Looking up EWS URL using Autodiscover for HIDDEN
    AUSFÜHRLICH: Using EWS on CAS https://outlook.office365.com/EWS/Exchange.asmx
    AUSFÜHRLICH: No AutoComplete Stream item found.
    AUSFÜHRLICH: No OWA AutoComplete item found.
    AUSFÜHRLICH: No Suggested Contacts folder found.
    AUSFÜHRLICH: No RecipientCache folder found.

    Can you explain me what no folder found means?!?
    Could the mailbox language (is german) be the reason or are the folders already empty?!?

    Thanks for advice!!!

    Denis

    Like

    • There is no ‘Well Known Folder’ equivalent for Suggested Contacts, so yes it’s picked based on the name ‘Suggested Contacts’. If the name in German locale is different, it won’t be found and therefor also not cleared.

      Like

  12. Has anyone had success disabling the autocomplete feature via Intune instead of GPO?

    HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Preferences\
    ShowAutoSug=0 (REG_DWORD)
    ^^Doesn’t seem to work consistently

    While the below fails when scripted due to the permissions on the policies keys
    HKEY_CURRENT_USER\Software\Policies\Microsoft\office\16.0\Outlook\Preferences\ShowAutoSug=0 (REG_DWORD)

    Like

  13. Hello,

    great script, good work, it worked for me. A question, is it possible to delete only one entry in the Automcomplete Stream?

    Best Regards,
    Florian

    Like

      • Ah okay,

        i found this snippet from here:

        https://community.spiceworks.com/topic/1836616-removing-single-outlook-autocomplete-entry-from-all-users

        ##### Convert the byte array and store as xmldoc. There seems to be a special character at the beginning so I don’t include it in the xml
        $stringData = [System.Text.Encoding]::UTF8.GetString($config.XmlData).Substring(1)
        $xmlDoc.LoadXml($stringData)
        $nodes = $xmlDoc.SelectNodes(“/AutoCompleteCache/entry”)
        write-host “converting array”
        #loop through each entry and remove if necessary
        foreach($node in $nodes)
        {
        if($node.smtpAddr -ne $null -and $node.smtpAddr -eq $badDomain)
        {
        $node.ParentNode.RemoveChild($node) | Out-Null
        write-host “`tRemoving: $($node.smtpAddr) from $($mailbox.PrimarySMTPAddress)”
        }
        else {write-host “`tNot Found $($node.smtpAddr) from $($mailbox.PrimarySMTPAddress)”}
        }

        Is it possible to put in this in your script?

        Best regards,
        Florian

        Like

    • That might happen if you use an authentication method not approved (blocked) by Exchange Online, e.g. requiring OAuth and using Basic Authentication (Security Defaults, Authentication Policy) or Conditional Access is at play. Consult the Azure AD Sign-In logs to look for clues.

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.