Powershell - create scheduled task in Windows Task Scheduler - part 2

Published on: 9th Dec 2019

Updated on: 23th Jul 2025

Overview

In part 1, we introduced the on demand task which is not a repetitive task. In part 2, we will show you how to create daily and week task.

The Powershell script in creating daily and weekly tasks

$task_folder = "\myTasks\"
$ps_script_file = "d:\temp5\test-script.ps1"

<###########################################
this is task #3 - DAILY
###########################################>

$task_name = "myTask3"

## The first line will show err if $task_folder does not exist.
## The second line will not show any error if $task_folder does not exist.
#$exist = Get-ScheduledTask -TaskPath $task_folder | Where-Object {$_.TaskName -eq $task_name}
$exist = Get-ScheduledTask | where {$_.TaskPath -eq "\myTasks\" -and $_.TaskName -eq $task_name }

if (!$exist)  {
    $axn = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass ""$ps_script_file"""

    $sch = New-ScheduledTaskTrigger -Daily -At 5pm

    Register-ScheduledTask -TaskName $task_name  -TaskPath $task_folder -Action $axn -Trigger $sch

    Write-Host "created new task $task_name"
}
else {
    Write-Host "$task_name task already exists"
}

<###########################################>

$task_name = "myTask3-1"
$exist = Get-ScheduledTask | where {$_.TaskPath -eq "\myTasks\" -and $_.TaskName -eq $task_name }

if (!$exist)  {
    $axn = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass ""$ps_script_file"""

    $sch = New-ScheduledTaskTrigger -Daily -At 1am

    $task = Register-ScheduledTask -TaskName $task_name  -TaskPath $task_folder -Action $axn -Trigger $sch

    # run every 30 minutes
    $task.Triggers.Repetition.Interval = "PT30M"

    # for 12 hours. For 1 days, use "P1D".
    $task.Triggers.Repetition.Duration = "PT12H"

    # update the task
    $task | Set-ScheduledTask

    Write-Host "created new task $task_name"
}
else {
    Write-Host "$task_name task already exists"
}

<###########################################
this is task #4 - WEEKLY
###########################################>

$task_name = "myTask4"
$exist = Get-ScheduledTask | where {$_.TaskPath -eq "\myTasks\" -and $_.TaskName -eq $task_name }

if (!$exist)  {
    $axn = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass ""$ps_script_file"""

    # runs at 6:30pm.
    $sch = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday, Friday -At "18:30"

    Register-ScheduledTask -TaskName $task_name  -TaskPath $task_folder -Action $axn -Trigger $sch

    Write-Host "created new task $task_name"
}
else {
    Write-Host "$task_name task already exists"
}

Advantages on Powershell in creating daily and weekly tasks

Disadvantages on Powershell in creating daily and weekly tasks

Conclusion

Even though we have spent a lot of hours in digging into Powershell on managing the task, it's worth it. Now, we are able to manage the task without touching the slow response GUI.

Related posts

  1. Create scheduled task in Windows Task Scheduler - part 1
  2. Create scheduled task in Windows Task Scheduler - part 2
  3. Create scheduled task in Windows Task Scheduler - part 3

Back to #POWERSHELL blog

Back to #blog listing

Author

Lau Hon Wan, software developer.