Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a scheduled task which runs a batch file which does some stuff. I want the batch file to kick off another batch file which runs in user space; i.e. %username% == the currently logged-in user, with user permissions, etc. How can I run a command under the current user, given that the scheduled task runs with SYSTEM permissions?

I need this for both Windows XP and Windows 7.

share|improve this question

Ever since the introduction of Terminal Services, "current user" can be plural. Even XP supports fast user switching.

The closest you can get is "user connected to the console session". For this, use WTSGetActiveConsoleSessionId() + WTSQueryUserToken() + CreateEnvironmentBlock() + CreateProcessAsUser().

I wrote this: – Compile, run with full path to your batch file as arguments.

Yes, this requires .NET Runtime, but it's likely that your systems already have it. The compiler's part of the Runtime, too: %SystemRoot%\Microsoft.NET\Framework64\v3.5\csc.exe (any version starting with v2.* will work).

Note: WTSQueryUserToken() requires the program to be running as LocalSystem. (According to the docs, SeTcbPrivilege is not enough, but I haven't checked.)

share|improve this answer
When this answer was posted, I didn't know about psexec yet. Using it might be easier. – grawity Aug 6 '13 at 11:40

Under "When Running Task, use the following user account:" you can set it to "BUILTIN\Users" group, it will run the task as the current logged in user.

share|improve this answer

I don't think you can set it through the task scheduler tool to dynamically use the current logged in user, but you can specify a user account to use besides System in the task itself. Both of the below options will require that you have the password for the username.

Windows 7: In the properties window for the task, there is a button in Security Options called "Change User or Group".

Windows XP: In the properties window for the task, you can enter a username in the "Run As" field.

One final option may be to create a startup script that will create the task when the user logs on, and a companion logoff script to delete it when they log off. The command is "Schtasks", which is nearly identical in XP and 7. Documentation for the command is available from MS at the following site:

share|improve this answer
I'm creating the task via the command line, so I can't change the run as user. Otherwise I would simply check that "run as currently logged on user" box which is terribly convenient! And regarding the logon/logoff script suggestion, that's pretty much what I intend to do, but the schtasks command cannot check the above-mentioned checkbox; it requires an username and password, and I don't want to prompt the user for their password from a script. – Ricket Mar 15 '11 at 14:27
Windows 7 does not seem to have the password entry requirement, at least when I just tried it with UAC off. For XP, you can try using the deprecated AT command instead. – Hyppy Mar 15 '11 at 14:33
But the AT command, even with the /interactive flag, runs the command in SYSTEM privileges. I've been testing it just this morning actually and I can't get it to run in the user environment. It seems there are a multitude of ways I could accomplish this in Windows 7, but the big issue at the moment is Windows XP. – Ricket Mar 15 '11 at 15:01

You must log in to answer this question.

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