My question is why should
i provide a start in path when clearly it says "Optional" in the
Let's say for instance that I am creating a scheduled task that will run a Powershell script:
Why is this wrong? Well first, we're not sure if powershell.exe will resolve correctly because we don't know if the user who executes the scheduled task has his or her PATH environment variables set up correctly. Depending on who you set the task to run as, be it System, or a domain user, their PATH variables will be different. Sometimes it works, and powershell.exe is launched properly, but why not go ahead and take the guesswork out of it and specify a fully qualified path name the powershell.exe executable.
Secondly, script.ps1 is where? You didn't specify. Powershell is going to launch and its startup directory will be
%WINDIR%\System32. Unless that is where you stored your script (and I hope it's not,) Powershell will not be able to find it.
This is better now that we've given fully qualified pathnames to both the executable and the script. But we still haven't gotten to what "Start in (optional)" is for. Well hang on, we're getting there.
The contents of
script.ps1 is thus:
"Hello world!" | Out-File test.txt
So if I were to run the above scheduled task now, which launches Powershell, which runs script.ps1, where do you think test.txt will be written?
If you guessed
%WINDIR%\System32, then you get a cookie!
If I wanted test.txt to go to
C:\Scripts instead, then I would type
C:\Scripts into the "Start in (optional)" field!
That's why it's optional.
I should also point out that you can also work around this by using fully qualified path names in your script to get things to go where you want them to go without having to worry about PATH variables and working directories.
Now on to the second question.
schtasks.exe does not appear to have the capability to add the "Start in" or "Working directory" of a scheduled task. Bummer.
One thing you can do is use the XML for Task Scheduler 2.0 tasks, which does let you set the working directory. Here is the XML for the test task that I created for this question:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
You can use
schtasks /XML to scriptomatically import tasks from XML files.