Scheduled tasks represent the central mechanism for executing programs at certain times or events. Since PowerShell 3.0 you can manage scheduled tasks with several cmdlets.

A freshly installed Windows already comes with a variety of pre-defined tasks for system maintenance. Important services such as the virus scanner, the backup program, and Windows Update depend on them.

Analyzing existing tasks

Over the course of time, third-party applications add more and more tasks usually used for automatically downloading updates. However, it is not always optimal if every unimportant tool continuously runs its own update service.

PowerShell is often more efficient than GUI programs if you want to analyze and change the settings and data of system components. Even though the Windows Task Scheduler supports all available task features, navigating through the numerous tabs is sometimes time-consuming.

Displaying scheduled tasks with Get-ScheduledTask

If you want to get an overview of the task states and authors, Get-ScheduledTask offers help. If you run the cmdlet without parameters, it displays all available tasks with their properties TaskPath, TaskName, and State.

TaskPath provides information about the tasks' position in the Task Scheduler's tree structure. You need the task location to access its details. Possible State values are Ready, Running, and Disabled.

Displaying task location and status with Get ScheduledTask

Displaying task location and status with Get ScheduledTask

For instance, to display all disabled Tasks, you have to execute this command:

Get-ScheduledTask | ? state -eq Disabled

Get-ScheduledTask allows you to restrict the query with the help of TaskName and TaskPath whereby both parameters support wildcards.

Get-ScheduledTask -TaskPath \Microsoft\Windows\Win*

Reading task details

If you need detailed information for a particular task, the Get-ScheduledTaskInfo cmdlet is useful. It will give you details such as the time since the last start, the number of missed runs, and the result of the last run.

Because Get-ScheduledTaskInfo requires you to specify TaskPath, it is easier if you call Get-ScheduledTask with the task's name and then pass the result to Get-ScheduledTaskInfo:

Get-ScheduledTask StartComponentCleanup | Get-ScheduledTaskInfo
Get ScheduledTaskInfo provides details for scheduled tasks

Get ScheduledTaskInfo provides details for scheduled tasks

You can also use the information that Get-ScheduledTaskInfo provides to filter certain tasks:

Get-ScheduledTask | Get-ScheduledTaskInfo | ? NumberOfMissedRuns -gt 0

This command displays all tasks with missed runs.

Disabling tasks

If you want to disable a particular task, for instance because it always fails, you can run the Disable-ScheduledTask cmdlet. Like Get-ScheduledTask, it accepts the parameters TaskPath and TaskName to restrict its application to certain tasks. But unlike Get-ScheduledTask, Disable-ScheduledTask doesn't support wildcards.

Disable-ScheduledTask -TaskName StartComponentCleanup

However, in most cases you will want to filter the list with certain criteria and then pass it to Disable-ScheduledTask:

Get-ScheduledTask | Get-ScheduledTaskInfo | ? NumberOfMissedRuns -gt 10 | Disable-ScheduledTask

Activating, stopping, starting, and deleting tasks

Activating, stopping, and continuing scheduled tasks works along similar lines. PowerShell provides the cmdlets Enable-ScheduledTask, Stop-ScheduledTask, and Start-ScheduledTask for this purpose. For example, to stop all currently running tasks, you can run this command:

Get-ScheduledTask | ? State -eq running | Stop-ScheduledTask

If you don't just want to stop or disable a task, but want to remove it completely, you can work with Unregister-ScheduledTask.

avatar
9 Comments
  1. Ray (Rank 2) 6 years ago

    Your command for getting the state doesn’t work, The name “state” is not a proper name, After exporting to a csv I was able to see that it should be scheduledtaskstate. The following line gave me clear output for just the name and whether it was enabled or not.

    Get-ScheduledTask | ? scheduledtaskstate -eq Enabled | Select-Object Taskname, scheduledtaskstate

    • If you look at the first screenshot, you will see that state is a valid property. Maybe you didn’t get any results because you have no scheduled task configured? Try “disabled” instead of “enabled.”

      • Ray (Rank 2) 6 years ago

        Whether or not it shows that the switch is valid, it is not working correctly. I have scheduled tasks enabled. Tested it on a system that has 4 scheduled tasks that I set myself when the server was built. I also have all the default MS Windows tasks in one or the other state. All of my manually created tasks are enabled and run at defined times every day. They show successful and are confirmed successful by me verifying that the tasks copied and deleted the log files.

        I’m running PS 4 or 5 on all my servers currently, and the one I tested on is using PS4 as shown below. It is above PS3 which is the version that includes this

        Major Minor Build Revision
        —– —– —– ——–
        4           0           -1        -1

        Doesn’t work when state is -eq Enabled, but pulls all MS Windows tasks when Disabled is selected. It does not pull any tasks from manually created scheduled tasks. It also does not pull any tasks MS Windows tasks that are enabled. The only way I have found to show all tasks correctly is the method I have provided in my first comment.

        Ray

         

        • @Ray,

          What is the OS version of the system where you scheduled your tasks?

          What is the output when you type Get-ScheduledTask |Get-Member

      • Ray (Rank 2) 6 years ago

        I have found the first problem in that “State” is “Ready” instead of “Enabled” which is MS being non-standard in naming. Scheduledtaskstate is “Enabled” or “Disabled”.

        Ray

        avatar
  2. Dustin 3 years ago

    Is there a way to display the configuration settings rather than just the schedule summary for a scheduled task like you can firewall rules (get-netfirewallrule -displayname [firewall rule])?

  3. Leo 2 years ago

    Is there any way to reset the counter for NumberofMissedRuns? I have some tasks that are at a very high number but are still able to run. I was hoping this number gets reset to 0 as soon as the task recovers and starts running again. 

    avataravatar
    • Leos Marek (Rank 4) 2 years ago

      As I checked there is no option to set it. The workaround could be to use export command and import the task again. 

      avatar
  4. D 2 years ago

    Anyway to get the Description of a task? It could be a helpful variable for me…

    Register-ScheduledTask -Action $Action -Trigger $trigger -TaskName "Computer $Computer" -Description $Description
    
    (Get-ScheduledTask -TaskName $Task).Description (?)

Leave a reply

Please enclose code in pre tags

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account