Recipe5.3.Running a Task When a User Logs On


Recipe 5.3. Running a Task When a User Logs On

Problem

You want to run a task when a user logs on to a system.

Solution

There 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.

Registry

There 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 values


Values 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

The values under the RunOnce keys are deleted once the tasks start.


Startup Folders

Similar 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

Discussion

By 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 Also

Recipe 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)



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net