I want to write a single script (one double klick) that automatically executes a predefined set of commands but with some controlled reboots in between to prevent interdependencies. How to accomplish that?


command 1
command 2
command 3

One way would be to create a registry key HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\ by pointing to script to run on startup. The scenario would be:

  • script01.bat does his job and write HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\ with value "path to script02.bat"

  • script02.bat does his job and write HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\ with value "path to script03.bat" and so long.

  • Would it be possible to achieve this within one single file?
    – Clacers
    Jun 1 '17 at 14:03
  • Not this way, maybe there is another one possibility. Jun 1 '17 at 14:04

This script is a very good solution! I tried it and can confirm it works!

However I'd recommend changing %~n0 and %~dpnx0 to "%~n0" and "%~dpnx0" to prevent Regedit Syntax errors.

@echo off
call :Resume
goto %current%
goto :eof

::Add script to Run key
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /d %~dpnx0 /f
echo two >%~dp0current.txt
echo -- Section one --
shutdown -r -t 0
goto :eof

echo three >%~dp0current.txt
echo -- Section two --
shutdown -r -t 0
goto :eof

::Remove script from Run key
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /f
del %~dp0current.txt
echo -- Section three --
goto :eof

if exist %~dp0current.txt (
    set /p current=<%~dp0current.txt
) else (
    set current=one
  • Couldn't this result in an endless boot loop if a problem occurs?
    – HackSlash
    Jun 8 '21 at 16:03

Look at BoxStarter. http://boxstarter.org

This should get you what you need for reboot and continue.

  • Not sure it's actually that complicated in the user interface - but if the script above works, use it. If it has issues and you start needing something more resilient with pending reboot checks and the like, you will end up writing something much more complex than what you get out of the box with Boxstarter. Something to keep in mind. HTH Jun 14 '17 at 12:58

Sorry for the late answer. Couldn't it be achieved with scheduled tasks ? Like create a scheduled task that runs once after reboot and launches the script where you want to resume it ?

  • Hm. That looks more like a comment asking for clarification than an answer. The idea is very good, though. Turn the questions into statements and maybe even show how it's done (I guess you're thinking of schtasks /create ...) and I'll happily upvote.
    – Stephan
    Jun 8 '21 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.