Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When setting up a scheduled task, for the action section I'm trying to access an environment variable for the location of the executable that I wish to run. This is mainly because on one machine the executable could be in C: on another it could be on D: I've verified that if I use a hard coded path my scheduled task runs, but if I change the action to point to "%MyAppPath%\MyApp.exe" the task won't start suffering a launch failure. I've verified that the environment variable is setup.

Is it even possible inside the action (and working directory) part of task schedule to reference an environment variable - is my syntax wrong.

On a side note, I did think about calling a BAT file and referencing the environment variable there, but that doesn't solve anything for me as the main issue is actually knowning whether the application (and the bat file too) is on C: or D: drives - inside the drive itself the path is the same.

UPDATE: It turns out you can use them quite easily, it just requires a machine reboot to pick up changes in particular circumstances.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

I thought that you could reference environment variables from the Task Scheduler, but having just tried, it doesn't look like you can.

The one exception appears to be %PATH% so would it be possible to add your MyAppPath value to the %PATH% collection on each machine, then then just call MyApp.exe from the task scheduler, where the machine will be able to resolve the fully-qualified path as required?

share|improve this answer
    
Interesting, I just tried with %PATH% and could not get it to work. At that point I noticed a stupid typo when I'd updated %PATH%. I fixed the typo and verified that PATH was OK in a DOS box, but task scheduled still reported the problem - indicating it was seeing the "bad" version of %PATH%. Rebooting was needed to pick up the change (as you can't restart task scheduler). At that point it turned out I could now use my original environment variable. So the upshot is that you can use environment variablees, but a full machine reboot is needed to pick up any changes/new environment variables –  Paul Hadfield Sep 2 '11 at 11:39
    
Will mark this as the answer as you were correct that you can use them but if task scheduler has run before you modified the job / added the reference to an environment variable, it will need a reboot of the machine. –  Paul Hadfield Sep 2 '11 at 11:41
    
See tsvayer's answer - you don't need to reboot the machine. Just the task scheduler process. –  Ben Challenor Sep 10 '13 at 11:25

You do not need a machine reboot. You should terminate Taskeng.exe and the next time scheduled task is run it will get an updated environment.

share|improve this answer
    
Thank you so mcuh! I've been struggling with it for days. I've been trying to restart the Scheduler service to no avail in Win7/Win2008 (need to run as SYSTEM to restart it), but this is so much simpler! –  Timur May 15 at 14:47

On a side note, I did think about calling a BAT file and referencing the environment variable there, but that doesn't solve anything for me as the main issue is actually knowning whether the application (and the bat file too) is on C: or D: drives - inside the drive itself the path is the same.

%~d0 will expand to the drive letter of where the batch file is located. (%~dp0 for drive+directory, and so on.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.