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.

Sample Registry Run key values

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

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.