Windows – How to get login username of the login that triggerred a task in the task scheduler

batch-filetask-schedulerwindowswindows 7

In the Windows 7 task scheduler, I have a task that runs when any user logs on. This task is run as Administrator so it can get the proper priviliges and runs a bat file.

I would like to pass in the username (userid) of the user that logged in, which triggerred the task, to the bat file. I need to run the bat file using the Administrator account which results in the environment variable USERNAME being set to Administrator instead of the username that logged in.

How do you get the userid of the user that logged in causing the task to trigger?

Best Answer

It's not ideal, but you could have a separate task that just writes the username to a log file at the same time, and correlate the logs later. For that matter, since the task runs at login, this information is already available in the event logs. If you're really desparate to not correlate these later, you could add code (or a step) to the existing task to pull the most recent login event.

Login Event Log

Looking through my own logs a little more closely, I see you want Event ID 4624, but only where the Logon Type is 2 or 7. I saw a number of Logon Type 5 events for system accounts doing various things. There's more info here:

http://www.windowsecurity.com/articles-tutorials/misc_network_security/Logon-Types.html