CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Manage Scheduled Tasks

We have created a scheduled Task in our previous Article “PowerShell – Create Scheduled Task“. PowerShell provides a set of other cmdlets, to manage the Scheduled Tasks. Note that, to work with these cmdlets, the ScheduledTasks module must be available on your System.

Start a Scheduled Task

PowerShell provides Start-ScheduledTask cmdlet to start a registered scheduled task. Below is the command to start the scheduled task “notepad.exe”; what we have created in our previous article “PowerShell – Create Scheduled Task“.

PS C:\> Start-ScheduledTask -TaskName "notepad.exe"

Observe that, the notepad application has been started once you run the above command.

Stop the Scheduled Task

Stop-ScheduledTask cmdlet is useful to stop the running instances of the registered scheduled task. Use the below command to stop our “notepad.exe” scheduled task:

PS C:\> Stop-ScheduledTask -TaskName "notepad.exe"

Observe that, notepad application will be closed after calling this command.

Get Scheduled Task Information

Get-ScheduledTaskInfo cmdlet is used to get the run-time information for a scheduled task.  The below command shows this:

PS C:\> Get-ScheduledTaskInfo -TaskName "notepad.exe"

LastRunTime        : 9/25/2018 10:10:40 PM
LastTaskResult     : 267014
NextRunTime        :
NumberOfMissedRuns : 0
TaskName           : notepad.exe
TaskPath           :
PSComputerName     :

Remove the registered scheduled task

To remove the scheduled task from the Task Scheduler use Unregister-ScheduledTask cmdlet. Once you run this command, PowerShell will prompt before it performs the action.

Check whether our task is registered

PS C:\> Get-ScheduledTask -TaskName "notepad.exe"

TaskPath                                       TaskName                          State
--------                                       --------                          -----
\                                              notepad.exe                       Ready

Remove the task

PS C:\> Unregister-ScheduledTask -TaskName "notepad.exe"

Confirm
Are you sure you want to perform this action?
Performing operation 'Delete' on Target '\notepad.exe'.
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y

Verify whether the task is removed

PS C:\> Get-ScheduledTask -TaskName "notepad.exe"
Get-ScheduledTask : No MSFT_ScheduledTask objects found with property 'TaskName' equal to 'notepad.exe'.  Verify the value of the property and retry.
At line:1 char:1
+ Get-ScheduledTask -TaskName "notepad.exe"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (notepad.exe:String) [Get-ScheduledTask], CimJobException
    + FullyQualifiedErrorId : CmdletizationQuery_NotFound_TaskName,Get-ScheduledTask

PS C:\>

Observe that, once the task has been removed; PowerShell throws ObjectNotFound message when you try to get the task details.

These are the commands useful to manage the scheduled tasks in PowerShell. Note that, you must have the ScheduledTasks module available in your system to use these commands.

We will discuss more PowerShell commands in my upcoming Articles.

[..] David

PowerShell – Manage Scheduled Tasks

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top