- How to change the PowerShell prompt - Wed, Mar 22 2023
- Trim characters from strings in PowerShell - Tue, Mar 14 2023
- Set Chrome, Firefox and Edge as default mail client (mailto handlers) - Mon, Mar 6 2023
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.
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
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.
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.
Join the 4sysops PowerShell group!
Your question was not answered? Ask in the PowerShell forum!
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.”
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.
What is the OS version of the system where you scheduled your tasks?
What is the output when you type Get-ScheduledTask |Get-Member
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”.
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])?
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.
As I checked there is no option to set it. The workaround could be to use export command and import the task again.
Anyway to get the Description of a task? It could be a helpful variable for me…