1

I have the requirement to create a scheduled tasks that runs once when a particular user logs on. The intention is to start a task only for a particular user.

I know that I can use the Task Scheduler to create a Task with a trigger At log on and under Settings, I specify the particular user.

However, because I need this created during an install routine, it must be created automatically, using the command line, for example using the schtasks command. It is possible to create a task executed at log on using the following example:

schtasks /Create /TR executable.exe /RU user /TN name /SC ONLOGON

But I did not find any modifier to specify a particular user for the ONLOGON trigger.

Resources:

Do you know if there is any undocumented switch? Or any other commandline tool that provides the necessary features?

Thanks.

Sven Plath
  • 558
  • 1
  • 5
  • 17

4 Answers4

4

Add the option "/IT" to the schtasks /create command, e.g:

schtasks /Create /TR executable.exe /RU user /TN name /SC ONLOGON /IT

As mentioned at https://technet.microsoft.com/en-us/library/cc725744(v=ws.11).aspx "the /it parameter to indicate that the task runs only when the (specific user) is logged on"

You should disregard the Task Scheduler GUI where "At log on of any user" is mentioned and actually try rebooting your device. You'll find that the specific command is only run when you log in with the (specific user)'s account.

Agrim
  • 471
  • 9
  • 19
3

Best way is to simply import an XML via the schtasks command line. Here is a sample that runs "calc.exe" as bob, whenever bob logs in:

  <?xml version="1.0" encoding="UTF-16" ?> 
- <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
- <RegistrationInfo>
  <Date>2014-10-22T01:50:13</Date> 
  <Author>user</Author> 
  </RegistrationInfo>
- <Triggers>
- <LogonTrigger>
  <StartBoundary>2014-10-22T01:50:00</StartBoundary> 
  <Enabled>true</Enabled> 
  <UserId>WIN7BOX\bob</UserId> 
  </LogonTrigger>
  </Triggers>
- <Principals>
- <Principal id="Author">
  <UserId>bob</UserId> 
  <LogonType>InteractiveToken</LogonType> 
  <RunLevel>LeastPrivilege</RunLevel> 
  </Principal>
  </Principals>
- <Settings>
  <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> 
  <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries> 
  <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> 
  <AllowHardTerminate>true</AllowHardTerminate> 
  <StartWhenAvailable>false</StartWhenAvailable> 
  <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> 
- <IdleSettings>
  <StopOnIdleEnd>true</StopOnIdleEnd> 
  <RestartOnIdle>false</RestartOnIdle> 
  </IdleSettings>
  <AllowStartOnDemand>true</AllowStartOnDemand> 
  <Enabled>true</Enabled> 
  <Hidden>false</Hidden> 
  <RunOnlyIfIdle>false</RunOnlyIfIdle> 
  <WakeToRun>false</WakeToRun> 
  <ExecutionTimeLimit>P3D</ExecutionTimeLimit> 
  <Priority>7</Priority> 
  </Settings>
- <Actions Context="Author">
- <Exec>
  <Command>calc.exe</Command> 
  </Exec>
  </Actions>
  </Task>
mubix
  • 171
  • 4
2

I'm not aware of any such option. Unfortunately the schtasks command does not cover all features Task Scheduler 2.0 provides. You could accomplish this with a VBScript using the Task Scheduler Scripting Objects, however, it deems me quite troublesome to (ab)use Task Scheduler for something that could be achieved far easier with a Startup shortcut, a registry entry in the user's Run key, or a logon script.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • So you say that an entry in the Run section is preferable to using the Task Scheduler? And I only should use the Scheduler for periodically running tasks? Thanks for pointing me into the right direction. I will go with a Registry Entry. – Sven Plath Mar 07 '13 at 08:53
  • @SvenPlath Of course that's just my personal opinion, but yes, I'd choose a `Run` entry over a scheduled logon task any day. – Ansgar Wiechers Mar 07 '13 at 11:51
-1

This works.

SchTasks /Create /SC DAILY /TN “abcMyTask” /TR “C:RunMe.bat” /ST 09:00 /IT /S /U testuser /P password@123

Create a new user (here I created testuser from my local Windows system). On your local machine it does not work. Try to run the command from another Windows system with the username testuser and password. It creates a scheduled task remotely.

splash
  • 13,037
  • 1
  • 44
  • 67
Jyoti
  • 27
  • 2