SharePoint 2013 - User Profile Service Stuck on Starting

There are really fantastic and well known articles on User Profile Configuration and Stuck on Starting issues. (These are applicable to SP2010 and SP2013)

These are listed below but can be located on Spencer Harbar's Site http://www.harbar.net http://www.harbar.net/articles/sp2010ups.aspx (User Profile Configuration)

http://www.harbar.net/articles/sp2010ups2.aspx (Stuck on Starting - detailing why and how to reset it)

http://www.harbar.net/articles/sp2010ups2.aspx#ups14 (How to reset the Sync status)

You need to follow his reset procedure first and as He has also stated, it is best to delete the User Profile Service application and create a new one from scratch.

Make sure that if you have installed Updates that all updates have been applied successfully on all servers before you create the new User Profile Service Application.

I decided to write up a very basic script as even after running the reset script detailed in Spencer's article the service still remained stuck on "Starting" and the status of the Service Instance remained as "Provisioning".

This therefore blocked starting the service on any other server and left the server I tried to start it on in a partial state as the Sync Service can only be started on 1 server in a farm.

The details below simply get a reference to your server and service instance and force it to be un-provisioned.

Copy out the following code below and run it in an Administrative PowerShell or PowerShell ISE window. (NB: This code is sample code and is run at your own risk - test on Dev and QA environments first)

########################################

Add-PSSnapin Microsoft.SharePoint.Powershell
$TypeName = "User Profile Synchronization Service"
$ServerName = "SERVERNAME" #Replace with your server name where the service is stuck on Starting
$Serviceinstance = Get-SPServiceInstance | where-object {$_.TypeName -eq $TypeName -and $_.Server.Address -eq $ServerName}
$Serviceinstance.Unprovision()

#######################################

Make sure that the user you are logged in as is a Farm Administrator.