Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I automatically run a script when I log on to Windows?

I'd like to run this whenever I log into my computer:

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

A bonus would be to be able to run the same script whenever I unlock the screen saver as well.

share|improve this question

4 Answers 4

up vote 17 down vote accepted

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.
share|improve this answer
    
Always nice to see somebody liked the answer :-D –  Ivo Flipse Jul 30 '09 at 14:07
    
i almost wanna turn this CW and tag it faq :) –  quack quixote Apr 20 '10 at 5:15
    
Be my guest @quack ;-) FAQ for what actually? –  Ivo Flipse Apr 20 '10 at 6:21

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.

share|improve this answer
5  
If you want to run this when any user logs on, you need to put it in the All Users startup folder (%allusersprofile%/Start Menu\Programs\Startup), or make an entry in the Run section of the LocalMachine reg hive: HKLM\Software\Microsoft\Windows\CurrentVersion\Run) –  Treb Jul 30 '09 at 13:34

A good alternate method is to create a batch file with the commands you want to run in them. Next you will specify in the policy editor on the machine that it run a login script as part of the login process. A quick note is this option is only available on XP Pro and not the home version.

Group Policy Editor Tutorial Here

share|improve this answer

Anyone know if this can be used to run when the screen is locked/unlocked as well?

There is a way for Windows 7 and/or Windows Server 2008.

Use Task Scheduler, create new basic task, as the Trigger use "When I log on".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.