Recipe 5.3. Running a Task When a User Logs OnProblemYou want to run a task when a user logs on to a system. SolutionThere are four ways you can make tasks run automatically after a user logs on: the Registry, startup folders, login scripts, and Group Policy. I explain how to use login scripts in Recipe 5.4 and Group Policy in Recipe 5.5. Here, I'll describe the Registry and startup folder options. RegistryThere are four Registry keys that you can use to run tasks automatically. To use any of the keys, simply create a value entry of type REG_SZ under the key. Give the value any name you want and specify the full path to the program or script and any parameters as the value data. See Figure 5-1 for some example entries. Figure 5-1. Sample Registry Run key valuesValues defined under this key cause tasks to run for every user that logs on to the system: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run Values defined under this key cause tasks to run whenever the user that is currently logged on logs on to the system: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run Values defined under this key cause a task to run the next time any user logs on to the system (and not after that): HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce Values defined under this key cause a task to run the next time the current user logs on to the system: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
Startup FoldersSimilar to the Run and RunOnce Registry keys, programs contained in the startup folders run after a user logs on. Generally, shortcut files (.lnk files), not the actual programs, are placed in the startup folders, but you can do it either way. Programs placed in the following folder are run after any user logs on to the system: %ALLUSERSPROFILE%\Start Menu\Programs\Startup Programs placed in this folder are run for a specific user: %USERPROFILE%\Start Menu\Programs\Startup DiscussionBy default, tasks defined under the Run and RunOnce keys are not executed if the system is booted into Safe mode. You can force tasks defined under the RunOnce keys to execute even if the system is booted into Safe mode by prefixing the value names under the RunOnce key with an asterisk (*). RunOnce value entries may also be prefixed with an exclamation point (!), which causes the associated task not to be deleted until after the program completes. Without this, each value entry is deleted before each task is executed. Multiple value entries under the Run or RunOnce keys and multiple programs in the Startup folders are loaded in an indeterminate order, so you can't assume one will run before another. If this is an issue for you, you could create a batch file that calls the programs in a particular order and put only the batch file in the Run or RunOnce key. There are several other Registry keys that can cause tasks to run automatically. For a complete list of all of these Registry keys and folders, see Recipe 5.6. See AlsoRecipe 5.6, MS KB 137367 (Definition of the RunOnce Keys in the Registry), MS KB 179365 (INFO: Run, RunOnce, RunServices, RunServicesOnce and Startup), and MB KB 314866 (A Definition of the Run Keys in the Windows XP Registry) |