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

I'd like to run something like 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.

  • accpeted answer is very high octane -- using your one-liner, I offer something more humble (but effective) below... ;-)
    – Jeff
    Aug 15, 2014 at 22:24

6 Answers 6


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://" &
    strGroups = LCase(Join(CurrentUser.MemberOf))
    If InStr(strGroups, ENGINEERING_GROUP) Then
        wshNetwork.MapNetworkDrive "g:",
    ElseIf InStr(strGroups, FINANCE_GROUP) Then
        wshNetwork.MapNetworkDrive "g:",
    ElseIf InStr(strGroups, HUMAN_RESOURCES_GROUP) Then
        wshNetwork.MapNetworkDrive "g:",
        "\\FileServer\Human Resources\"
    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.
  • Always nice to see somebody liked the answer :-D
    – Ivo Flipse
    Jul 30, 2009 at 14:07
  • i almost wanna turn this CW and tag it faq :) Apr 20, 2010 at 5:15
  • Be my guest @quack ;-) FAQ for what actually?
    – Ivo Flipse
    Apr 20, 2010 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


registry key.

  • 10
    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, 2009 at 13:34
  • I run a .exe through this way, but is there a way for it to run quietly? Without opening and keeping open a cmd?
    – bzupnick
    Mar 19, 2015 at 18:59
  • Where is "your startup folder"?
    – cja
    Aug 24, 2020 at 0:45
  • %userprofile%/Start Menu\Programs\Startup is folder for current user. @cja paste that (or the all users above) into your windows file explorer and it will redirect you to the actual folder.
    – bendecko
    May 7, 2021 at 14:53

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


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

  • You cannot use a basic task if you want the trigger to be "On workstation unlock"
    – Jeff
    Aug 15, 2014 at 22:13

No need! Windows already has a mechanism to log this for you in the event log. The event ID is 4624 (528 on pre-Vista systems).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .