[Bat] How to make a batch that is automatically executed after restarting


Hello!This time, as one of the countermeasures for the case where restart is required during processing, I tried to summarize how to make a batch that is automatically executed after restart.

There are a lot of processes that require a reboot

There are many opportunities to use batches when installing modules, such as initial Windows setup and customization, and vulnerabilities.
In some cases, you will be required to reboot when you actually apply the software, drivers, Windows updates, etc.So let's create a batch that will work even in situations where a reboot is required.

In this site, when you copy "" ", it may be full-width.In that case, please replace it with a half-width "" "after pasting.

Use the RunOnce registry

If you simply want it to run after a reboot, using the RunOnce registry will suffice.
This time, we will use HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Windows \\ CurrentVersion \\ RunOnce on the assumption that the user will execute it after logging on.

For the RunOne registryherePlease refer to.

Reboot judgment using arguments

You can add arguments to the RunOnce registry when registering the script.
By giving the label you want to jump to, you can move to the required processing after restarting.
The first argument of the batch is stored in% 1. (The second argument is% 2, the third argument is% 2 ...

Try using RunOnce

reg add "HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Windows \\ CurrentVersion \\ RunOnce" / v "Run after reboot" / t REG_SZ / d "% ~ 0 AfterReboot1"/ f
*% ~ 0 is a variable that returns its own full path

Then write the decision at the very beginning of the batch.
This allows the process to be executed step by step in one batch.
It's convenient for distribution.

if "% 1" neq "" goto:% 1

It should be like this.
* It did not work unless there were "" "at both ends of% 1 in the conditional expression of IF.

Creation example

Now, here is the code compiled based on the above.

Sample code that continues processing even after restarting

@ Echo off
cd / d% ~ dp0
Reboot judgment
if "% 1" neq "" goto:% 1
"Processing required to restart"
reg add "HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Windows \\ CurrentVersion \\ RunOnce" / v "Run after reboot" / t REG_SZ / d "% ~ 0 AfterReboot1" / f
Post-reboot processing
: AfterReboot1
"What you want to do after rebooting"

Please rewrite the process according to the content.


  • The RunOnce registry is useful for running after a reboot
  • You can complete the process by yourself by using% ~ 0 and the argument

It is a technique that can be used for various automated processes, so please take advantage of it.

I hope it helps you.


Translate »
I copied the title and URL