Script to fix issues described in KB947215 (PowerShell)
Introduction
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.
Scenario
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.
Script
This script contains the following advanced functions:
- Get-OSCUserSID
- Repair-OSCUserAccountProfile
- Remove-OSCInvalidUserSIDRegKey
You can use this script in the following ways (You need run the script as administrator):
Method 1:
- Download the script and copy it to your computer.
- Open the script file by using Notepad or any other script editors.
- Scroll down to the end of the script file, and then add the code to call the functions.
- Save the file and then run the script on the computer.
Method 2:
- Rename scriptname.ps1 to scriptname.psm1 (PowerShell Module file)
- Run the Import-Module cmdlet to import this module file in PowerShell Console.
Import-Module filepath\scriptname.psm1
To obtain the detailed information about how to use the functions, run the following command to retrieve the help information: Get-Help functionName -detailed
For example:
Get-Help Remove-OSCInvalidUserSIDRegKey –detailed
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.
PowerShell
Edit|Remove
powershell
$OSCSIDRegKeys = Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
$OSCSIDRegKeys | ForEach-Object {
# Create the $OSCSID object
$OSCSID = new-object PSObject
$OSCSID | Add-Member NoteProperty SID $null
$OSCSID | Add-Member NoteProperty SIDProfileName $null
$OSCSID | Add-Member NoteProperty SIDProfilePath $null
$OSCSID | Add-Member NoteProperty Invalid $false
$OSCSID.SID = $_.PSChildName
$OSCProfilePath = (Get-ItemProperty -Path $_.PSPath).ProfileImagePath
$OSCSID.SIDProfileName = $OSCProfilePath.SubString($OSCProfilePath.LastIndexOf('\') + 1)
$OSCSID.SIDProfilePath = $OSCProfilePath
$OSCSID.Invalid = -not (Test-Path $OSCProfilePath)
# 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(-not($OSCOmitSIDs -contains $OSCSID.SID)){
$OSCSIDs += $OSCSID
}
}
$OSCSIDRegKeys = Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
$OSCSIDRegKeys | ForEach-Object{
# Create the $OSCSID object
$OSCSID = new-object PSObject
$OSCSID | Add-Member NoteProperty SID $null
$OSCSID | Add-Member NoteProperty SIDProfileName $null
$OSCSID | Add-Member NoteProperty SIDProfilePath $null
$OSCSID | Add-Member NoteProperty Invalid $false
$OSCSID.SID = $_.PSChildName
$OSCProfilePath = (Get-ItemProperty -Path $_.PSPath).ProfileImagePath
$OSCSID.SIDProfileName = $OSCProfilePath.SubString($OSCProfilePath.LastIndexOf('\') + 1)
$OSCSID.SIDProfilePath = $OSCProfilePath
$OSCSID.Invalid = -not (Test-Path $OSCProfilePath)
# Omit bellow SIDs:
# s-1-5-18for NT AUTHORITY\SYSTEM
# s-1-5-19for NT AUTHORITY\LOCAL SERVICE
# s-1-5-20for NT AUTHORITY\NETWORK SERVICE
If(-not($OSCOmitSIDs -contains $OSCSID.SID)){
$OSCSIDs += $OSCSID
}}
Examples
Example 1: Get all users account information in the computer
Command: Get-OSCUserSID
Screenshot:

Example 2: Get a specific user’s account information by user SID
Command: Get-OSCUserSID –SID <User SID>
Screenshot:

Example 3: Get a specific user’s account information by user profile name
Command: Get-OSCUserSID –ProfileName <User Profile Name>
Screenshot:

Example 4: Repair a user account profile by user SID
Command: Repair-OSCUserAccountProfile –SID <User SID>
Screenshot:

Example 5: Delete an invalid SID registry key by user SID
Command: Remove-OSCInvalidUserSIDRegKey -SID <User SID>
Screenshot:

Example 6: Delete an invalid SID registry key by user profile name
Command: Remove-OSCInvalidUserSIDRegKey -ProfileName <User Profile Name>
Screenshot:

Example 7: Delete all invalid SID registry keys in the computer
Command: Remove-OSCInvalidUserSIDRegKey
Screenshot:

Prerequisites
Windows PowerShell 2.0
Additional Resources
Technical Resources: