Search code examples
windowsbatch-filecmdregistrywmic

Renaming/Deleting keys in the registry via batch cmd


When a user in Windows 7 logs in and the profile is corrupt they get a blank profile. I can fix easily enough by going to the registry and deleting the temporary profile, setting the refcount and state values. I'm getting stuck in my batch as my extraction from the registry is adding 2 spaces to the registry key name once I call for it in the batch. If I echo %SID% it returns the correct value without visible space. The registry does have %SID% and %SID%.bak

@echo off
setlocal ENABLEDELAYEDEXPANSION
for /F "skip=1 delims=" %%j in ('wmic useraccount where name^="%username%" get SID') do (
  set SID=%%j
  goto :DONE
)
:DONE
echo  SID=%SID%

reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%SID%.bak"
pause

The result of the batch:

 SID=S-1-5-21-1559011707-81249799-2450556423-1000
Permanently delete the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Window
s NT\CurrentVersion\ProfileList\S-1-5-21-1559011707-81249799-2450556423-1000  .b
ak (Yes/No)? y
ERROR: The system was unable to find the specified registry key or value.
Press any key to continue . . .

As you can see the output is putting 2 spaces after the SID & the file extension

Any ideas?


Solution

  • Just add this:

      set SID=!SID: =!
    

    this will delete all space!