Run application or script on Windows RDC connection

To my knowledge Windows does not provide a built-in method for running a program or script on connection, only log on. You'll need to find a program that registers with Windows (using WTSRegisterSessionNotification) and listens for the WM_WTSSESSION_CHANGE message.

A program that does the above will receive notifications from Windows about user logons, logoffs, connections, disconnections, and all of the events needed to launch a script at connection as well as log on.

Off the top of my head I don't know of any applications (other than pcAnywhere) that can do this.


You can make a new scheduled task and define the trigger to "At the moment of a new connection to a user session"


Use trigger "On connection to user session"

Note: I only noticed that the question is tagged "XP"/"Server2008". What's here may only work on Vista/2008R2 onwards.

Windows task scheduler has a predefined trigger for that. It is called On connection to user session.

It also allows you to distinguish between users and between local or remote connections.

The default when creating this trigger: Any user and Connection from remote computer. This seems to be what you want, so you don't have to change that.

Screenshot here: Screenshot of trigger

Source: Microsoft TechNet: Task Scheduler | Triggers (Archived here.)

Further reading

When you export this task and then look inside the file, you will see that this a trigger of type SessionStateChangeTrigger: Trigger detail There is in depth documentation on MSDN:

  • MSDN: SessionStateChangeTrigger object (Archived here.)
  • MSDN: SessionStateChangeTrigger.StateChange property (Archived here.)