How to setup local Autodiscover XML file to points to On-Prem Exchange solving office 365 persistence redirecting Autodiscover URL to the cloud.

Outlook 2016 / 2019 / 2021 / 2024 / Microsoft 365 connected to an on-premises Exchange keeps finding autodiscover.outlook.com instead of your own server. This guide shows how to point Outlook at the on-prem Autodiscover with a local Autodiscover XML file, using the domain example.com and the server autodiscover.example.com as an example. A ready-made script is at the end. It asks for the user’s email address and configures everything from that, including Outlook profiles that also have a Microsoft 365 mailbox.

Why Outlook goes to Office 365

Starting with Outlook 2016, Autodiscover first tries the Office 365 endpoint (Direct Connect to Office 365), and only then SCP and DNS. If the domain is verified in any Microsoft 365 / Entra tenant (even one used only for Teams or Intune), or an autodiscover DNS record is a CNAME to autodiscover.outlook.com, Outlook never reaches your Exchange server.

CauseHow to check
Domain is verified in an M365 tenant (Direct Connect)Autoconfiguration log shows the Office 365 endpoint first
autodiscover CNAME to autodiscover.outlook.comnslookup autodiscover.example.com (inside and outside)
Cached “last known good” URL in the profile%LOCALAPPDATA%\Microsoft\Outlook\16\*.xml
Hybrid leftovers on the user objectmsExchRemoteRecipientType / targetAddress
Wrong SCPGet-ClientAccessService | fl Name,AutoDiscoverServiceInternalUri

With a local XML, the lookup order becomes:

Office 365 endpoint→ Local XML (redirect)→ https://autodiscover.example.com→ Exchange 2019 settings

Before you start

  • autodiscover.example.com must resolve to the Exchange server (or load balancer) and be included in the Exchange certificate. If it is not, use mail.example.com in the URL.
  • Open https://autodiscover.example.com/autodiscover/autodiscover.xml in a browser. A login prompt or HTTP 401 is expected and means the service is alive.
  • All settings are per user (HKCU). Run them as the Outlook user, not as a different admin account.

Step 1: Create the local XML file

The XML holds no fixed server settings. It is a redirect, so Outlook still gets MAPI/HTTP and EWS settings from Exchange itself, and the file works for every user in the domain.

Create the folder %LOCALAPPDATA%\Autodiscover inside the user’s profile (C:\Users\<user>\AppData\Local\Autodiscover). The user owns that folder, so no admin rights are needed. Save the XML there as autodiscover-example.com.xml (UTF-8). Name one file per domain, so a user with several on-prem domains gets one XML for each.

<?xml version="1.0" encoding="utf-8"?>
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
  <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
    <Account>
      <AccountType>email</AccountType>
      <Action>redirectUrl</Action>
      <RedirectUrl>https://autodiscover.example.com/autodiscover/autodiscover.xml</RedirectUrl>
    </Account>
  </Response>
</Autodiscover>

Step 2: Registry settings

Key: HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\AutoDiscover (16.0 covers Outlook 2016, 2019, 2021 and Microsoft 365).

Value nameTypeDataPurpose
PreferLocalXMLDWORD1Use the local XML file
example.comString (REG_SZ)C:\Users\<user>\AppData\Local\Autodiscover\autodiscover-example.com.xmlMaps the SMTP domain to its XML file
ExcludeLastKnownGoodURLDWORD1Ignore the cached O365 URL
ExcludeHttpsRootDomainDWORD1Skip https://example.com/autodiscover (the website)
ExcludeExplicitO365EndpointDWORD1 = on-prem mailboxes only
0 = Outlook also has a Microsoft 365 mailbox
Skip the Office 365 lookup. This setting applies to every account in Outlook. See the next section.

ExcludeScpLookup is not set. SCP stays on, which is the default and correct for domain-joined PCs.

Note: example.com is a String Value, not a key. Its name must exactly match the part of the email address after the @. Add one value per SMTP domain if users have more than one.

The same settings as a .reg file for on-prem only. Replace USERNAME with the user’s folder name. Backslashes are doubled in .reg syntax only.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\AutoDiscover]
"PreferLocalXML"=dword:00000001
"ExcludeLastKnownGoodURL"=dword:00000001
"ExcludeHttpsRootDomain"=dword:00000001
"ExcludeExplicitO365Endpoint"=dword:00000001
"example.com"="C:\\Users\\USERNAME\\AppData\\Local\\Autodiscover\\autodiscover-example.com.xml"

Outlook with an additional Microsoft 365 account

Many users have their on-prem mailbox and a Microsoft 365 mailbox in the same Outlook. The Autodiscover registry values are global: they apply to every account in Outlook, not to a single domain. So:

  • ExcludeExplicitO365Endpoint=1 turns off Direct Connect for the Microsoft 365 account too. That account then falls back to DNS lookups, which are slower, and it fails if the tenant domain’s autodiscover CNAME isn’t set up.
  • It isn’t needed for the on-prem domain anyway. With PreferLocalXML=1, Outlook checks the local XML first, before the Office 365 endpoint, for any domain that has a mapping.

Co-existence settings: PreferLocalXML=1 plus the domain mapping, and ExcludeExplicitO365Endpoint=0. The on-prem domain uses the local XML, and the Microsoft 365 domain has no mapping, so it uses Direct Connect as usual.

The script detects this case itself. It reads the accounts in all Outlook profiles and checks each domain against Microsoft’s login realm service (getuserrealm.srf). If any account is in a Microsoft 365 tenant, co-existence mode is offered as the default. It also logs whether the on-prem domain itself is registered in a tenant, which is the usual reason Outlook went to Office 365 in the first place.

Step 3: Clear the cache and re-add the account

  1. Close Outlook completely (check Task Manager).
  2. In %LOCALAPPDATA%\Microsoft\Outlook\16\, delete the cached Autodiscover *.xml files that contain @example.com. If there’s no Microsoft 365 account in Outlook, you can delete them all.
  3. If the on-prem account was already created against Office 365, remove only that account (File → Account Settings → Remove) and add it again. Other accounts, including Microsoft 365, can stay. If Outlook has only that one account, creating a new profile is cleaner: Control Panel → Mail → Show Profiles → Add.

Step 4: Verify

  1. Open Outlook, hold Ctrl, right-click the Outlook icon in the system tray, and choose Test E-mail AutoConfiguration.
  2. Enter the email address and password, uncheck both Guessmart options, and click Test.
  3. On the Log tab you should see the local XML being used and a redirect to autodiscover.example.com. On the Results tab, the server URLs should be your Exchange, not outlook.office365.com.

On the Connection Status window (Ctrl + right-click the tray icon), the server column should show your on-prem server and the protocol HTTP (MAPI over HTTP).

Automated script (download)

The ZIP contains a PowerShell script and a launcher that do steps 1–3 for the logged-on user, for any domain:

  1. Asks for the email address. On a domain-joined PC it suggests the address from Active Directory, so the user can just press Enter.
  2. Finds the server. It takes the domain from the address and tests https://autodiscover.<domain> and https://mail.<domain>. A 401 response counts as success. The user picks one, or types another host name.
  3. Checks for Microsoft 365. It looks for Microsoft 365 accounts in the Outlook profiles and chooses on-prem only or co-existence mode.
  4. Writes the XML. It creates %LOCALAPPDATA%\Autodiscover\ in the user’s profile and writes autodiscover-<domain>.xml there.
  5. Writes the registry values for that domain and mode.
  6. Clears the cache. It closes Outlook (after asking) and deletes only the cached Autodiscover files for that domain.
  7. Logs everything to %TEMP%\Set-LocalAutodiscover.log.

Run it again with another address to add more on-prem domains. /remove rolls back.

⬇ Download Outlook-Local-Autodiscover.zip

Usage (extract all files to one folder first):

:: Simple - double-click, as the logged-on user (NOT "Run as administrator")
Setup-Autodiscover.cmd

:: Undo everything the script configured
Setup-Autodiscover.cmd /remove

# PowerShell - no prompts, server and mode chosen automatically
powershell -ExecutionPolicy Bypass -File .\Set-LocalAutodiscover.ps1 -Email user@example.com -Force

# Fixed server, Outlook also has a Microsoft 365 mailbox
powershell -ExecutionPolicy Bypass -File .\Set-LocalAutodiscover.ps1 `
  -Email user@contoso.com -AutodiscoverHost mail.contoso.com -Mode CoexistM365

# Remove one domain only
powershell -ExecutionPolicy Bypass -File .\Set-LocalAutodiscover.ps1 -Remove -Email user@contoso.com
ParameterMeaning
-EmailMailbox address. If omitted, the script asks (and suggests the address from AD).
-AutodiscoverHostServer host name. If omitted, autodiscover. and mail. + domain are tested.
-ModeAuto (default), OnPremOnly or CoexistM365
-ForceNo prompts, for GPO. Uses -Email, or the AD mail attribute.
-RemoveRoll back all domains, or only the one given with -Email

Mass deployment with GPO

  • Script: run Set-LocalAutodiscover.ps1 -Force as a User Logon script (User Configuration → Windows Settings → Scripts → Logon). It reads each user’s address from the AD mail attribute, so one GPO covers everyone. Add -Mode CoexistM365 if users also have Microsoft 365 mailboxes.
  • Or with Group Policy Preferences:
    • Use Files to copy the XML to %LOCALAPPDATA%\Autodiscover\autodiscover.xml.
    • Use Registry (User Configuration) to create the values from Step 2.
  • For the policy path, use HKCU\Software\Policies\Microsoft\Office\16.0\Outlook\AutoDiscover. Users can’t change it, and it takes precedence over the preference path.

Long-term fix: the local XML is a client-side workaround. Also fix the cause:

  • Remove any autodiscover CNAME that points to outlook.com.
  • Set the SCP: Set-ClientAccessService -Identity EX01 -AutoDiscoverServiceInternalUri https://autodiscover.example.com/autodiscover/autodiscover.xml
  • If the domain must stay verified in M365, keep ExcludeExplicitO365Endpoint=1 in GPO permanently.

Troubleshooting and rollback

SymptomFix
Still connects to Office 365Old profile. Create a new one, and check the domain value name matches the address exactly.
Certificate warningThe URL host name is not on the Exchange certificate. Use the name that is (for example mail.example.com).
Endless password promptsCheck Autodiscover/MAPI virtual directory authentication (NTLM/Negotiate) and the URLs in Get-MapiVirtualDirectory.
XML ignoredThe settings were written to the wrong user’s HKCU (the script was run elevated as an admin). Run it as the user.
Script blockedUse the .cmd launcher (it uses -ExecutionPolicy Bypass), or right-click the files → Properties → Unblock.

Rollback: run Setup-Autodiscover.cmd /remove, or delete the values above from the registry and delete the XML file.

In short, for end users

If Outlook can’t find your company mailbox or keeps asking you to sign in to Microsoft 365, this fix tells Outlook where your company mail server is.

  1. Download the file Outlook-Local-Autodiscover.zip.
  2. Extract the ZIP: right-click it, choose Extract All, then click Extract.
  3. Double-click Setup-Autodiscover.cmd. If Windows shows “Windows protected your PC”, click More info → Run anyway.
  4. Type your work email address and press Enter. If your address is already shown in brackets, just press Enter.
  5. For the other questions, press Enter to accept the suggested answer. If you also use a Microsoft 365 mailbox in Outlook, answer Y when asked.
  6. If you’re asked to close Outlook, press Enter. Wait for the green “Done” message, then press any key.
  7. Open Outlook. If it still doesn’t connect, contact IT support. They may need to remove your work account and add it again.

Your email, contacts and calendar are not deleted, and other mail accounts in Outlook keep working. Only the way Outlook finds your work mail server changes.

Leave a Reply

Your email address will not be published. Required fields are marked *