This sample can help you resolve the issue, “You receive a 'The User Profile Service failed the logon' error message”. This script shows how to list user SID information and delete invalid SID registry keys by using PowerShell Script.
When you log on to a Windows 7-based or a Windows Vista-based computer by using a temporary profile, you receive the following error message:
The User Profile Service failed the logon. User profile cannot be loaded.
To resolve this issue, use the script described in this sample to remove the invalid SID registry key.
You can find more All-In-One Script Framework script samples at https://aka.ms/onescriptingallery