Specifying the running directory for Scheduled Tasks using schtasks.exe
Asked Answered
R

7

49

I have an application which gets called by a scheduled task. It moved from Windows Server 2003 to Windows Server 2008. On 2003, the app ran in the directory where the executable was located. On 2008 Environment.CurrentDirectory (C#) reports that it's running in C:\Windows\System32. How do I set the running directory? I'm using schtasks.exe for command-line deployment.

UPD: Through the interface, it seems to be the "Start in (optional)" field on the action edit screen.

UPD: Looks like using the XML file may help, but I'm looking to do without it.

Rook answered 15/1, 2009 at 17:52 Comment(0)
A
18

I recently came across the same issue. The way I resolved it was to add the /V1 switch to the schtasks command.

/V1 creates a pre-vista compatible scheduled task and automatically populates the Start In directory.

Africanize answered 21/4, 2009 at 14:2 Comment(3)
works like a charm, as long as your working dir is not just the drive letter (c:\test.bat was setting working dir to c: which is ignored)Rook
Yes - but by using /V1, you can no longer created a task that runs under the SYSTEM account (/RU SYSTEM), or you'll receive the error message ERROR: The task has been configured with an unsupported combination of account settings and run time options.Rai
It appears as though when using /V1, it is not possible to add tasks to task folders by specifying /TN '\TaskFolder\TaskName' since schtasks will return error: "The Task Name may not contain the characters: < > : / \ |"Enrico
L
97

Just wanted to add details that are valid for Windows Server 2008 and 2012. As many people can understand screen shots better here is a screen shot:
enter image description here

To sum it up. When you create the action for your scheduled task you have the option to set the "Start in (optional)" field (rounded in red on the screen shot). This will be the directory from where your process is triggered.

Lynden answered 10/12, 2014 at 12:35 Comment(4)
I believe the OP wanted to use schtasks.exe, not the Task Scheduler.Ethelyn
Yes, you are right, the question is actually for schtasks.exe. Apologies for missing this last year.Lynden
Though the OP wasn't looking for this, it helped me solve my problem. +1 for that. :)Hegelianism
I think it is worth noting that the path needs a trailing `` to be recognized.Heidyheifer
A
18

I recently came across the same issue. The way I resolved it was to add the /V1 switch to the schtasks command.

/V1 creates a pre-vista compatible scheduled task and automatically populates the Start In directory.

Africanize answered 21/4, 2009 at 14:2 Comment(3)
works like a charm, as long as your working dir is not just the drive letter (c:\test.bat was setting working dir to c: which is ignored)Rook
Yes - but by using /V1, you can no longer created a task that runs under the SYSTEM account (/RU SYSTEM), or you'll receive the error message ERROR: The task has been configured with an unsupported combination of account settings and run time options.Rai
It appears as though when using /V1, it is not possible to add tasks to task folders by specifying /TN '\TaskFolder\TaskName' since schtasks will return error: "The Task Name may not contain the characters: < > : / \ |"Enrico
S
9

You can set the start in directory using the following command

The key is the \ in the /tr switch.

SCHTASKS /Create /u username /p pswd /ru "NT AUTHORITY\SYSTEM"
  /rp /sc ONSTART /tn task-name /tr "\"D:\name-of-file-to-run\" "
Seldun answered 15/1, 2009 at 17:52 Comment(6)
Wondering if anyone can shed some light on this one?Westernmost
This works great... You essentially escape the quotes from the command prompt so it treats them as a string, the argument then passed into the \tr parameter is "D:\name-of-file-to-run" which maintains spaces etc etc. (Note you do not need the space between the last two "")Sheers
This method doesn't work on Windows 8.1 nor in Windows 2008 R2 StandardKolnick
Is there something missing in this answer? E.g. if I want to run a program at "c:\window\my.exe" and the start in directory should be in "c:\temp", how should I do?Elastomer
this does not work on windows7, using Angel Naydenov EG how do you set the "Start in (optional)" parmaeter? while also doing Program/script: and "Add arguments(optional):" asked a similar Q hereAntipyretic
Not seeming to get this to work. The \" seems to be the escaped double-quotes, in which case the schtasks help mentions that you can use a single-quote ' instead as the inner quote. The solution here helps you to run an application from a specific folder, but the working directory of that application is still not set to that folder. You can test this with a .bat batch file that has the pause command, and see which directory it stops in.Almena
I
8

Please see my answer to a similar question, of how to set the "Wake the computer to run this task..." option that is only available from the Task Scheduler UI (and via the XML), and not the schtasks.exe /create command line .

The nuts and bolts of it are:

  1. Create your task via schtasks.exe /create /tn MyTask ...
  2. Export your task to XML via schtasks.exe /query /xml /tn MyTask > MyTask.xml
  3. Update this XML via XSLT or a search/replace
  4. Re-import (overwriting the old task) via schtasks.exe /create /tn MyTask /xml MyTask.xml /f

Details are here.

Ido answered 6/2, 2009 at 18:45 Comment(2)
Yeah, that's the approach I was talking about in my second update. I guess "can't be done" is a valid answer, so I'll accept.Rook
According to the answer <#448274> you are both wrong.Prism
L
3

I hope people will see this answer for the XML approach (frankly I think that it's a cleaner method and there's some better documentation around what parameters you can set to configure specific features within the task too).

Step 1: create XML file that sets all task settings, several places for more info on XML elements:

Step 2: Specific to "where" the task will execute from (as in the starting directory the script will commence in the command line, this is directly related to the OP's question....You'll need to configure the parameter like so...

<?xml version="1.0" encoding="UTF-16"?> 
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> 
  <RegistrationInfo> 
    <Date>2012-08-19T16:49:14.6182</Date> 
    <Author>YOUR-COMPUTER-DOMAIN\YOUR-USERNAME</Author> 
  </RegistrationInfo>

    ... a bunch of other stuff in between here ....

  <Actions Context="Author"> 
    <Exec> 
      <Command>C:\PythonEXE\mini_program_test.exe</Command> 
      <Arguments></Arguments> 
      <WorkingDirectory>C:\Some\where\here\</WorkingDirectory> 
    </Exec>
  </Actions> 
</Task> 

Please note above that there aren't any quotation marks in the WorkingDirectory paramater -- I made that mistake earlier.

Step 3: Since you'll be using schtasks.exe in order to CREATE this new task via the XML, take a look here for more info: https://msdn.microsoft.com/en-us/library/bb736357.aspx

Step 4: In the windows command line, you'll execute something like this (once your XML is ready)

C:\>schtasks /CREATE /TN "TASK-NAME-HERE" /RU "YOUR-USERNAME" /RP "YOUR-PASSWORD" /XML C:\YOUR-XML-FILE-LOCATION\ready.xml

Hope this helps out a bit -- have fun!

Lussi answered 1/8, 2017 at 23:35 Comment(0)
E
2

Try cd /d "<WorkingDirectory>" & schtasks <SchtasksParams>

Change working directory and then run schtasks.

Erleena answered 30/12, 2011 at 7:58 Comment(2)
This method works on Windows Server 2008, but doesn't work on Windows Server 2008 R2.Erleena
This method doesn't work on Windows 8.1 nor in Windows 2008 R2 StandardKolnick
E
-1

Use My.Application.Info.DirectoryPath in App without need a XML Setting.

Entrepreneur answered 28/1, 2013 at 5:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.