@ECHO OFF
:: Fix and Delete Duplicate Profiles 1.1.17
:: Copyright 2005, `felix`
:: SILENT option added by esas
:: Last updated: March 10, 2005
IF %OS%!==! GOTO:EOF
setlocal
::Check for options (RESTART or SILENT)
If /I "%1"=="RESTART" SET RS=YES
If /I NOT "%1"=="RESTART" SET RS=NO
if %RS%==NO goto CheckmoreRS
if %RS%==YES goto SkipCheckmoreRS
:CheckmoreRS
If /I "%2"=="RESTART" SET RS=YES
If /I NOT "%2"=="RESTART" SET RS=NO
:SkipCheckmoreRS
If /I "%1"=="SILENT" SET SL=YES
If /I NOT "%1"=="SILENT" SET SL=NO
if %SL%==NO goto CheckmoreSL
if %SL%==YES goto SkipCheckmoreSL
:CheckmoreSL
If /I "%2"=="SILENT" SET SL=YES
If /I NOT "%2"=="SILENT" SET SL=NO
:SkipCheckmoreSL
set theNum=0
set KEY=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
set gtid=getsid \\%computername% %username% \\%computername% %username%
for /f "Tokens=7" %%i in ('%gtid%^') do (set UserSID=%%i)
set qry1=reg query "%KEY%" /v ProfilesDirectory
set qry2=reg query "%KEY%" /s
set qry3=reg query "%KEY%\%USERSID%" /v ProfileImagePath
set fnd1=FIND /I "Documents and Settings"
set fnd2=FIND /I "ProfileImagePath"
set fnd3=FIND /I "AllUsersProfile"
set fnd4=FIND /I "DefaultUserProfile"
set fnd5=FIND /I "LocalService"
set fnd6=FIND /I "NetworkService"
set fnd7=FIND /I "%UserName%"
for /f "Tokens=1,2*" %%i in ('%qry1%^|%fnd1%') do (set DocSetDir0=%%k)
CALL:CheckCurrentLocal
CALL:CheckIfDuplicate
if %RS%==YES call:restartIt
GOTO:EOF
:CheckCurrentLocal
:: Check the registry to and make sure the profiles are correct based
:: a totally clean default installation. If duplicate profiles are
:: found based on reinstall or lost of user account information, we
:: correct the paths to the correct directories here.
FOR /f "Tokens=1,2*" %%i in ('%qry2%^|%fnd3%') do if /i not "%%k"=="All Users" (
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v AllUsersProfile /d "All Users" /f >NUL )
FOR /f "Tokens=1,2*" %%i in ('%qry2%^|%fnd4%') do if /i not "%%k"=="Default User" (
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v DefaultUserProfile /d "Default User" /f >NUL )
FOR /f "delims=\ Tokens=1,2*" %%i in ('%qry2%^|%fnd5%') do if /i not "%%k"=="LocalService" (
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-19" /v ProfileImagePath /d "%DocSetDir0%\LocalService" /f >NUL )
FOR /f "delims=\ Tokens=1,2*" %%i in ('%qry2%^|%fnd6%') do if /i not "%%k"=="NetworkService" (
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-20" /v ProfileImagePath /d "%DocSetDir0%\NetworkService" /f >NUL )
FOR /f "delims=\ Tokens=1,2*" %%i in ('%qry2%^|%fnd7%') do if /i not "%%k"=="%UserName%" (
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%UserSID%" /v ProfileImagePath /d "%DocSetDir0%\%UserName%" /f >NUL )
GOTO:EOF
:CheckIfDuplicate
::Let the user know something is happening unless SILENT option in used...
if %SL%==YES goto Skippopup
popup /YN /HF "userprofilechecker.hlp" /IS /M "Duplicate User Profile Detected" /T "User Profile Checker 1.1.16"
if errorlevel 7 EXIT
:Skippopup
:: Now we actually check againist the registry for duplicates
:: Normally they will be username.computername, all users.windows,
:: default user.windows, localservice.nt authority and networkservice.nt authority.
:: the last 3 are hidden.
CALL:Header
FOR /f "Tokens=1,2*" %%i in ('%qry2%^|%fnd3%') do if /i "%%k"=="All Users" (CALL:DoIt "All Users" "%DocSetDir0%")
FOR /f "Tokens=1,2*" %%i in ('%qry2%^|%fnd4%') do if /i "%%k"=="Default User" (CALL:DoIt "Default User" "%DocSetDir0%")
FOR /f "delims=\ Tokens=1,2*" %%i in ('%qry2%^|%fnd5%') do if /i "%%k"=="LocalService" (CALL:DoIt "LocalService" "%DocSetDir0%")
FOR /f "delims=\ Tokens=1,2*" %%i in ('%qry2%^|%fnd6%') do if /i "%%k"=="NetworkService" (CALL:DoIt "NetworkService" "%DocSetDir0%")
FOR /f "delims=\ Tokens=1,2*" %%i in ('%qry3%^|%fnd7%') do if /i "%%k"=="%UserName%" (CALL:DoIt "%UserName%" "%DocSetDir0%")
CALL:Footer
GOTO:EOF
:Header
:: We output the list of directories to the log so we have a record
:: of the directories deleted and to check the script loaded.
> c:\AllUsersProfilefix.log echo Fix and Remove Duplicate Profiles 1.1.17
>> c:\AllUsersProfilefix.log echo Copyright 2005, `felix`
>> c:\AllUsersProfilefix.log echo www.simplify-i-t.com
>> c:\AllUsersProfilefix.log echo Automatically Generated %DATE:~-10%.
>> c:\AllUsersProfilefix.log echo --------------------------------------------------------------------------------
>> c:\AllUsersProfilefix.log echo.
>> c:\AllUsersProfilefix.log echo This computer was found to have one or more duplicate system or user profiles.
>> c:\AllUsersProfilefix.log echo.
>> c:\AllUsersProfilefix.log echo The following registry key has been updated with restored values.
>> c:\AllUsersProfilefix.log echo "%KEY%"
>> c:\AllUsersProfilefix.log echo.
GOTO:EOF
:Footer
>> c:\AllUsersProfilefix.log echo.
>> c:\AllUsersProfilefix.log echo --------------------------------------------------------------------------------
>> c:\AllUsersProfilefix.log echo This issue is possibly caused by the following reasons:
>> c:\AllUsersProfilefix.log echo * The computer operating system has been reinstalled.
>> c:\AllUsersProfilefix.log echo * The computer operating system has been restore from a back-up image.
>> c:\AllUsersProfilefix.log echo.
>> c:\AllUsersProfilefix.log echo For more information please see Microsoft Knowledge Base Article link below:
>> c:\AllUsersProfilefix.log echo
http://support.micro...kb;en-us;314045
>> c:\AllUsersProfilefix.log echo.
GOTO:EOF
:Doit
:: We check to make sure it is not a valid user directory and
:: unhide all the directories that are hidden first so we can
:: build the list to remove them.
for /f "usebackq delims= tokens=1,2" %%i IN (`dir /a /b "%~2\%~1*"`) DO IF /i "%%i" NEQ "%~1" attrib -a -s -h "%~2\%%i"
for /f "usebackq delims= tokens=1,2" %%i IN (`dir /ad /b "%~2\%~1*"`) DO IF /i "%%i" NEQ "%~1" call:flagIt %~2\%%i
GOTO:EOF
:flagIt
:: The list of duplicate/unrequired directories is built
:: and added to the Local Machine RunOnce key to be removed
:: at next boot.
:: The directories can not be removed before a reboot because the
:: system still has them loaded even know we have changed the paths
:: in the registry.
set /a theNum=%theNum%+1
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" /v RemoveDuplicateProfile%theNum% /d "%comspec% /C RMDIR /S /Q """%*"""" /f >NUL
>> c:\AllUsersProfilefix.log echo %theNum%. %* has been deleted!
GOTO:EOF
:restartIt
shutdown -r -f -t 90 -c
"%computername% needs to restart to finish the update to the user profiles.
"
GOTO:EOF
endlocal