Unable to Start User Profile Synchronization Service

Recently I was at a customer site and ran into an issue with the User Profile Synchronization Service failing to start. We found the following error in the SharePoint ULS logs:

UserProfileApplication.SynchronizeMIIS: Failed to configure MIIS post database, will attempt during next rerun. Exception: System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception

The problem ended up being a PowerShell profile that had been created for the SharePoint Farm account. The fact that the PoweShell profile existed was fine but the customer was mapping drives within the profile. The drive mapping was the culprit. We were able to verify this by following these steps:

  1. Stop the User Profile Service and delete the User Profile Service Application.
  2. Verify you have a PowerShell profile. Check out the Windows PowerShell Owner's Manual on TechNet (https://technet.microsoft.com/en-us/library/ee692764.aspx) for a reference on checking for, adding, and editing a PowerShell profile. If you have a PowerShell profile then comment out everything and save the file.
  3. Clear the SharePoint Configuration Cache. There's a great post written by James Waymire on this (https://blogs.msdn.com/b/jamesway/archive/2011/05/23/sharepoint-2010-clearing-the-configuration-cache.aspx). This post was written for SharePoint 2010 but it will also work for SharePoint 2013.
  4. Create a new User Profile Service Application.
  5. You should be able to start the User Profile Service and the User Profile Synchronization Service.

 I have seen a lot of posts on similar issues with the User Profile Synchronization Service not starting but none that resolved our problem. Hopefully this will help others having this problem.