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 the VBScript.
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.
This script contains one function, DeleteInvalidSID. You can use this script in following way:
Method:
Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.
For Each subKey In subKeys
' Check if a “NT AUTHORITY” user SID is specified
' Omit bellow SIDs:
' s-1-5-18 for NT AUTHORITY\SYSTEM
' s-1-5-19 for NT AUTHORITY\LOCAL SERVICE
' s-1-5-20 for NT AUTHORITY\NETWORK SERVICE
If subKey <> "S-1-5-18" And subKey <> "S-1-5-19" And subKey <> "S-1-5-20" Then
ObjReg.GetExpandedStringValue HKEY_LOCAL_MACHINE, KeyPath & "\" & subKey, ValueName, profileImagePath
' If a SID with a invalid profileImagePath, this SID is invalid, and it need to be deleted
If Not ObjFSO.FolderExists(profileImagePath) Then
ObjReg.DeleteKey HKEY_LOCAL_MACHINE, KeyPath & "\" & subKey
WScript.Echo "Invalid SID: [" & subKey & "] was deleted successfully!"
End If
End If
Next
For Each subKey In subKeys ' Check if a “NT AUTHORITY” user SID is specified ' Omit bellow SIDs: ' s-1-5-18 for NT AUTHORITY\SYSTEM ' s-1-5-19 for NT AUTHORITY\LOCAL SERVICE ' s-1-5-20 for NT AUTHORITY\NETWORK SERVICE If subKey <> "S-1-5-18" And subKey <> "S-1-5-19" And subKey <> "S-1-5-20" Then ObjReg.GetExpandedStringValue HKEY_LOCAL_MACHINE, KeyPath & "\" & subKey, ValueName, profileImagePath ' If a SID with a invalid profileImagePath, this SID is invalid, and it need to be deleted If Not ObjFSO.FolderExists(profileImagePath) Then ObjReg.DeleteKey HKEY_LOCAL_MACHINE, KeyPath & "\" & subKey WScript.Echo "Invalid SID: [" & subKey & "] was deleted successfully!" End If End If Next
Example 1 Repair a user account profile by specifying the user SID.
Code: RepairUserAccountProfile (<SID>)
Screenshot:

Example 2: Delete all invalid SID registry keys in the computer.
Code:
DeleteInvalidSIDRegKey()
Screenshot:
Technical Resources:
You receive a "The User Profile Service failed the logon” error message