For context, I have a script that runs a procedure, then reboots the computer, after reboot one directory needs to be recursively removed.
Is this possible to do by using a "built-in" Windows 10 command, by calling directly or inline, as a registry value in runOnce registry key pair?
11 Answer
A script can insert a RUNONCE key in the registry for an action to be done only once on reboot or login of a specific user. Administrator permissions are required for updating the registry.
The registry keys are:
- On reboot :
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce - Current user :
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
For the RunOnce key to be deleted only if its task is run successfully,
prepend the key name value with an exclamation mark !.
If you want the RunOnce command to run even in Safe Mode, prepend the
key name value with an asterisk *.
The RunOnce script should not insert new RunOnce entries in the registry.
Examples:
For reboot
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" /v RunScript /t REG_SZ /d "\"C:\\setup\\script.cmd\""For current user (HKEY_CURRENT_USER)
reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v RunScript /t REG_SZ /d "\"C:\\setup\\script.cmd\"" /fFor a specific user (HKEY_USERS)
reg add HKU\S-1-5-21-3492930481-2827506072-2794401122-1001\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v RunScript /t REG_SZ /d "\"C:\\setup\\script.cmd\"" /fAll user profiles
FOR /D %%D IN ("C:\Users\*") DO IF EXIST "%%D\NTUSER.DAT" REG LOAD HKU\TEMP "%%D\NTUSER.DAT" && (reg add HKU\TEMP\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v Reg_Value /t REG_SZ /d Reg_Data /f & REG UNLOAD HKU\TEMP)