Relationship between PowerShell and MS Task Scheduler

DanYeung W 41 Reputation points
2023-02-27T18:02:42.3533333+00:00

I found some random issues on MS Task Scheduler jobs. These are couple examples: (1) some jobs (not all) had error when creating an Excel files if the directories had space. (2) sometimes (not all) it couldn't find the Excel file in a location and returned "file not found". The same Python programs ran from VS Code didn't have problems.

Therefore, I decided to try using PowerShell Scheduled Task. After I created a task and Get-ScheduledTask, I saw the Task Scheduler jobs in the list. I thought these are two different apps. My questions are

  1. What is the relationship between PowerShell and MS Task Scheduler? Why Task Scheduler jobs show up in PowerShell list?
  2. If I use PowerShell Scheduled Task, can it fix the errors? I would have tried this if I didn't see Task Scheduler jobs showed up in PowerShell command. Thanks.
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,328 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 51,346 Reputation points
    2023-02-27T19:25:31.1333333+00:00

    Powershell is just a CLI to do work on the system. You can do just about anything with PS including reading/writing files, updating registry, making HTTP calls and whatnot. It is equivalent to writing the program code yourself or using CMD, it's just more flexible.

    PS does support working with scheduled tasks. There is an entire cmdlet available for it as discussed here. Through that cmdlet you can interact with TS and do things like get the tasks, run tasks, created or edit tasks, etc. Behind the scenes the cmdlet is just calling the scheduled task API that the UI also uses.

    1. If you run Get-ScheduledTask then it'll return the same list of tasks as you see in the UI. If you add or change tasks via PS then the UI will show the updates as well. PS is simply calling the task API like the UI.
    2. You can use PS to create or update tasks but if you cannot get it to work in the UI then using PS isn't going to solve your problem. The issue is most likely with your scheduled task and not how you're creating it.

    Without further information it would be hard to say but some common issues I see is trying to specify a path without wrapping it in quotes. So, for example, if you need to pass a path to a command then doing something like /arg somepath in the arguments field might work when it doesn't have spaces but fails when it does. Wrap the path in quotes. /arg "some path".

    Another issue I have seen is that the working directory for the command is not set. You should always set the working directory of the command you're trying to run to the directory where you expect it to run from. Task Scheduler runs as a service so the default is the system directory. This tends to mess up relative path-based code.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. DanYeung W 41 Reputation points
    2023-02-27T20:21:20.37+00:00

    Thanks @Michael Taylor , I would like to confirm if I understood correctly. MS Task Scheduler is the UI of PowerShell Scheduler Task? Thanks.


  2. DanYeung W 41 Reputation points
    2023-02-27T20:24:10.78+00:00

    Thanks @Michael Tayler ,
    I would like to confirm I understood correctly. MS Task Scheduler is the UI of PowerShell Scheduler Task?
    Thanks.

    0 comments No comments