How to Modify, Schedule and Launch Scheduled Tasks in Windows 8 and Server 2012
We’ve been looking at how to manage scheduled tasks in Windows 8 and Windows Server 2012 using PowerShell. In part one of this series, I showed you how to use the ScheduledTasks module to retrieve information about scheduled tasks. In part two, I walked you through creating a new scheduled task using PowerShell that would run daily.
Let’s see what else we can do with it. In this final installment, we’re going to learn how to manage scheduled tasks in Windows 8 and Windows Server 2012, specifically, how to modify a scheduled task, manually launch a scheduled task, disable and enable a task, and remove a scheduled task.
First, I’ll save the task to a variable so I don’t have to keep getting it.
PS C:\> $task = Get-ScheduledTask DailyMSInfo PS C:\> $task TaskPath TaskName State -------- -------- ----- \MyCompany\Reporting\ DailyMSInfo Ready
The task object has a number of properties that can be set as you can see in Figure 1.
Read the Best Personal and Business Tech without Ads
Staying updated on what is happening in the technology sector is important to your career and your personal life but ads can make reading news, distracting. With Thurrott Premium, you can enjoy the best coverage in tech without the annoying ads.
How to Modify a Scheduled Task
I think the easiest way to modify a scheduled task is to assign new values to the scheduled task object. Some properties you can modify like this:
PS C:\> $task.Author = "Jeff Hicks" PS C:\> $task.Description = "create a daily MSINfo32 Report" PS C:\> $task.version = "1.0" Some properties are part of the nested Settings object. PS C:\> $task.settings AllowDemandStart : True AllowHardTerminate : True Compatibility : Vista DeleteExpiredTaskAfter : DisallowStartIfOnBatteries : True Enabled : True ExecutionTimeLimit : PT72H Hidden : False IdleSettings : MSFT_TaskIdleSettings MultipleInstances : IgnoreNew NetworkSettings : MSFT_TaskNetworkSettings Priority : 7 RestartCount : 0 RestartInterval : RunOnlyIfIdle : False RunOnlyIfNetworkAvailable : False StartWhenAvailable : False StopIfGoingOnBatteries : True WakeToRun : False DisallowStartOnRemoteAppSession : False UseUnifiedSchedulingEngine : False MaintenanceSettings : volatile : False PSComputerName :
Many of these are properties of the task you see in the Task Scheduler. I’ll go ahead and modify some of these.
PS C:\> $task.settings.Compatibility = "Win8" PS C:\> $task.Settings.RunOnlyIfNetworkAvailable = $True
These changes are for the task held in my variable. To apply them to the scheduled task itself I’ll use Set-ScheduledTask.
PS C:\> $task | set-scheduledtask -user "globomantics\administrator" -Password "[email protected]"
You have to reapply the credentials the task is going to use. In Part 2 of this series I showed you some other ways of handling credentials.
But now when I refresh the task in Task Scheduler I can see my changes (Figure 2).
How to Manually Launch a Scheduled Task
If necessary, you can also manually launch a scheduled task from PowerShell. You either need to know the task name and path:
PS C:\> start-scheduledtask -taskname DailyMSInfo -TaskPath MyCompany\Reporting
Or an easier approach is to get the task, or tasks first and then pipe them to Start-ScheduledTask.
PS C:\> get-scheduledtask DailyMSInfo | Start-ScheduledTask
How to Disable and Enable a Scheduled Task
Should the need arise, it is simple to disable a scheduled task. As with Start-ScheduledTask you either need to specify the name and path, or simply pipe a task to Disable-ScheduledTask.
PS C:\> get-scheduledtask DailyMSInfo | Disable-ScheduledTask TaskPath TaskName State -------- -------- ----- \MyCompany\Reporting\ DailyMSInfo Disabled
The change is immediate. If you get the task from a remote computer it will be disabled on that computer.
Need to turn it back on? I bet you can guess how to do it. Here’s how to enable a scheduled task:
PS C:\> get-scheduledtask DailyMSInfo -CimSession chi-dc03 | Enable-ScheduledTask
It really is that easy.
How to Remove a Scheduled Task
Finally, you may have a need to totally remove a scheduled task. For that we unregister the task.
PS C:\> get-scheduledtask DailyMSInfo -CimSession chi-dc03,chi-win8-01 | Unregister-ScheduledTask -WhatIf What if: chi-dc03: Performing operation 'Delete' on Target '\MyCompany\Reporting\DailyMSInfo'. What if: chi-win8-01: Performing operation 'Delete' on Target '\MyCompany\Reporting\DailyMSInfo'.
As you can see, this cmdlet supports –Whatif which is helpful if you are running this against a number of machines. To truly remove the scheduled task I can re-run the command omitting –Whatif. PowerShell will prompt you for confirmation.
Managing scheduled tasks with PowerShell is pretty straightforward. There are a few other features that I didn’t cover but these articles should be enough to get you started. Unfortunately, as of the time of preparing these articles, the help content for the ScheduledTask module has not been released, so you might still need to do a little experimentation. If you run into issues, feel free to use the forums at PowerShell.org.