In this blog we provide an example of how we can create a scheduled task using PowerShell or Schtasks.
Create a Scheduled Tasking using PowerShell
As a minimum, a scheduled task needs an action (something to perform) and a trigger (when to perform it). We can define a scheduled task that launches
msedge.exe
with an argument of
https://www.alkanesolutions.co.uk
at 3pm every day like so (you may need to elevate your code, depending upon the triggers and actions that you specify):
$trigger = New-ScheduledTaskTrigger -Daily -At 3:00PM
$action = New-ScheduledTaskAction -Execute "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" -Argument "https://www.alkanesolutions.co.uk"
Register-ScheduledTask -TaskName "AlkaneTask" -Trigger $trigger -Action $action -Description "Alkane scheduled task"
But as we know, there are various other configurations that we might want to customise such as execution time limits, whether it is hidden, or whether we want it to run if only powered by batteries and so on. We can adapt our simple scheduled task using PowerShell like so:
$trigger = New-ScheduledTaskTrigger -Daily -At 3:00PM
$action = New-ScheduledTaskAction -Execute "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" -Argument "https://www.alkanesolutions.co.uk"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -Hidden -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Minutes 10)
Register-ScheduledTask -TaskName "AlkaneTask" -Trigger $trigger -Action $action -Settings $settings -Description "Alkane scheduled task"
There are many more options to explore for each cmdlet. For example, we may want to add multiple triggers for our Scheduled Task using PowerShell:
$trigger1 = New-ScheduledTaskTrigger -Daily -At 3:00PM
$trigger2 = New-ScheduledTaskTrigger -Daily -At 4:00PM
$action = New-ScheduledTaskAction - -Execute "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" -Argument "https://www.alkanesolutions.co.uk"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -Hidden -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Minutes 10)
Register-ScheduledTask -TaskName "AlkaneTask" -Trigger @($trigger1,$trigger2) -Action $action -Settings $settings -Description "Alkane scheduled task"
Create a Scheduled Tasking using PowerShell and Schtasks.exe
Sometimes, coding a scheduled task using PowerShell can seem fiddly. Especially if you’re an inexperienced scripter. So another option is to manually create the scheduled task interactively, then export the scheduled task to an XML file and create the scheduled task using Schtasks.exe!
Open Task Scheduler (you can search for taskschd.msc if you need to). Right-click the Task Scheduler Library and create your scheduled task with all relevant configurations.
Once done, right-click your scheduled task and click Export. Then save it as an XML file (we’ll save it to a file called
C:\Alkane\SchedTask.xml
). Then we can simply run the following to create our scheduled task using schtasks:
#create scheduled task
$returnmessage = (schtasks /Create /TN "AlkaneTask" /XML "C:\Alkane\SchedTask.xml")
write-host $returnmessage
and run the following to delete our scheduled task using schtasks:
$returnmessage = (SCHTASKS /Delete /TN "AlkaneTask" /F)
write-host $returnmessage
Whether we need to schedule routine maintenance scripts, backups, or any other task, PowerShell provides a powerful mechanism for automating these processes.