Automatically run a script when I log on to Windows

You can create a scheduled task that will run when your computer is unlocked:

  1. Start > Administrative Tools > Task Scheduler
  2. left pane: select Task Scheduler Library
  3. right pane: click Create Task... (NOTE: this is the only way to get the correct trigger)
  4. in the Create Task dialog:
    • General tab -- provide a name for your task
    • Triggers tab -- click New... and select On workstation unlock
    • Action tab -- click New... and click Browse... to locate your script
    • Conditions tab -- uncheck Start the task only if the computer is on AC power

Also, I tweaked the path in the script to read

echo %USERNAME% logged on at %DATE% %TIME% >> %USERPROFILE%\log.txt

If you already have a script:

To assign a logon script to a user or group

  • Open Computer Management.
  • In the console tree, click Users.

Where? System Tools/Local Users and Groups/Users

  • Double-click the user to which you want to assign a logon script.
  • Click the Profile tab.
  • In the Logon script field, enter the path and name of the logon script you want to assign to that user, and then click OK.

Else here's a handy "HowTo" from Microsoft

Creating logon scripts

You can use logon scripts to assign tasks that will be performed when a user logs on to a particular computer. The scripts can carry out operating system commands, set system environment variables, and call other scripts or executable programs. The Windows Server 2003 family supports two scripting environments: the command processor runs files containing batch language commands, and Windows Script Host (WSH) runs files containing Microsoft Visual Basic Scripting Edition (VBScript) or Jscript commands. You can use a text editor to create logon scripts. Some tasks commonly performed by logon scripts include:

  • Mapping network drives.
  • Installing and setting a user's default printer.
  • Collecting computer system information.
  • Updating virus signatures.
  • Updating software.
  • The following example logon script contains VBScript commands that use Active Directory
  • Service Interfaces (ADSI) to perform three common tasks based on a user's group membership:

It maps the H: drive to the home directory of the user by calling the WSH Network object's MapNetworkDrive method in combination with the WSH Network object's UserName property. It uses the ADSI IADsADSystemInfo object to obtain the current user's distinguished name, which in turn is used to connect to the corresponding user object in Active Directory. Once the connection is established, the list of groups the user is a member of is retrieved by using the user's memberOf attribute. The multivalued list of group names is joined into a single string by using VBScript's Join function to make it easier to search for target group names.

If the current user is a member of one of the three groups defined at the top of the script, then the script maps the user's G: drive to the group shared drive, and sets the user's default printer to be the group printer. To create an example logon script

  • Open Notepad.
  • Copy and paste, or type, the following:

    Const ENGINEERING_GROUP     = "cn=engineering"
    Const FINANCE_GROUP         = "cn=finance"
    Const HUMAN_RESOURCES_GROUP = "cn=human resources"
    
    Set wshNetwork = CreateObject("WScript.Network")
    wshNetwork.MapNetworkDrive "h:",
    "\\FileServer\Users\" & wshNetwork.UserName
    
    Set ADSysInfo = CreateObject("ADSystemInfo")
    Set CurrentUser = GetObject("LDAP://" &
    ADSysInfo.UserName)
    strGroups = LCase(Join(CurrentUser.MemberOf))
    
    If InStr(strGroups, ENGINEERING_GROUP) Then
    
        wshNetwork.MapNetworkDrive "g:",
        "\\FileServer\Engineering\"
        wshNetwork.AddWindowsPrinterConnection
        "\\PrintServer\EngLaser"
        wshNetwork.AddWindowsPrinterConnection
        "\\PrintServer\Plotter"
        wshNetWork.SetDefaultPrinter
        "\\PrintServer\EngLaser"
    
    ElseIf InStr(strGroups, FINANCE_GROUP) Then
    
        wshNetwork.MapNetworkDrive "g:",
        "\\FileServer\Finance\"
        wshNetwork.AddWindowsPrinterConnection
        "\\PrintServer\FinLaser"
        wshNetWork.SetDefaultPrinter
        "\\PrintServer\FinLaser"
    
    ElseIf InStr(strGroups, HUMAN_RESOURCES_GROUP) Then
    
        wshNetwork.MapNetworkDrive "g:",
        "\\FileServer\Human Resources\"
        wshNetwork.AddWindowsPrinterConnection
        "\\PrintServer\HrLaser"
        wshNetWork.SetDefaultPrinter
        "\\PrintServer\HrLaser"
    
    End If
    
  • On the File menu, click Save As.

  • In Save in, click the directory that corresponds to the domain controller's Netlogon shared folder (usually SystemRoot\SYSVOL\Sysvol\DomainName\Scripts where DomainName is the domain's fully qualified domain name).
  • In Save as type, click All Files.
  • In File name, type a file name, followed by .vbs, and then click Save. WSH uses the .vbs extension to identify files that contain VBScript commands.

The simplest way I can think of would be to put this in a .bat file in your startup folder.

A more complicated way would be to add the batch file to the registry in the

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

registry key.