Search code examples
batch-filehiveregistry

Access denied error unloading hive


Windows 7 batch file - I want all current users to execute a "runonce"

  • %1 is a username from a for loop
  • %2 is a value name
  • %3 is the data for the value
REG LOAD HKU\TEMP "%1\NTUSER.DAT"
REG ADD HKU\TEMP\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v %2 /t REG_SZ /d %3 /f
REG UNLOAD HKU\TEMP

Access denied error when reg unload executes. I run as admin, logged in as local admin. No other errors.

Can you provide a simple resolution?


Solution

  • There is a possibility that the Reg UnLoad may try to run before the new data has completed being added. I have added a Timeout command in the example code below to give it a little longer.

    @Echo Off
    Set "RKL=SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
    Set "RKV=%~1"
    Set "RKD=%~2"
    Set "RKP=Temp"
    
    For /F "UseBackQTokens=1*Delims==" %%A In (`WMIC Path Win32_UserProfile Where^
     "Loaded!='True' And Special!='True'" Get LocalPath /Value 2^>Nul`
    ) Do For %%C In ("%%B") Do (
        Reg Load "HKU\%RKP%" "%%~C\NTUSER.DAT"
        Timeout 2 /NoBreak>Nul
        Reg Add "HKU\%RKP%\%RKL%" /V "%RKV%" /D "%RKD%" /F>NUL
        Timeout 2 /NoBreak>Nul
        Reg UnLoad "HKU\%RKP%"
    )
    

    To use the code above, replace the %~1 and %~2 with the non double quoted strings you were previously sending as %2 and %3 and try again.