Create Scheduled Task or Scheduled Job to Indefinitely Run a PowerShell Script Every 5 Minutes

Here are two straight-forward ways to create Scheduled Tasks within Windows using PowerShell. One script creates a PowerShell Scheduled Job, and the other creates a classic Windows Scheduled Task. You can read about the differences here. PowerShell v3 and above required.

Create a Scheduled Job

The following script creates a PowerShell Scheduled Job which shows up in the Task Scheduler GUI, and also generates files located at:

$home\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs\\Output

I included the options that I most often use — repeat every X indefinitely, run as specified user, and run with highest privileges. Note that you should always use service accounts or accounts without expiring or changing passwords when setting up Scheduled Jobs and Scheduled Tasks.

# Change these three variables to whatever you want
$jobname = "Recurring PowerShell Task"
$script =  "C:\Scripts\Test-ExampleScript.ps1 -Server server1"
$repeat = (New-TimeSpan -Minutes 5)

# The script below will run as the specified user (you will be prompted for credentials)
# and is set to be elevated to use the highest privileges.
# In addition, the task will run every 5 minutes or however long specified in $repeat.
$scriptblock = [scriptblock]::Create($script)
$trigger = New-JobTrigger -Once -At (Get-Date).Date -RepeatIndefinitely -RepetitionInterval $repeat
$msg = "Enter the username and password that will run the task"; 
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)

$options = New-ScheduledJobOption -RunElevated -ContinueIfGoingOnBattery -StartIfOnBattery
Register-ScheduledJob -Name $jobname -ScriptBlock $scriptblock -Trigger $trigger -ScheduledJobOption $options -Credential $credential

This job will appear under Task Scheduler -> Task Scheduler Library -> Microsoft -> Windows -> PowerShell -> ScheduledJobs.

taskscheduler

These Scheduled Jobs can be managed within PowerShell using cmdlets.

Create a Scheduled Task

This script will generate a new Scheduled Task. As previously stated, I included the options that I most often use — repeat every X indefinitely, run as specified user, and run with highest privileges. Note that you should always use service accounts or accounts without expiring or changing passwords when setting up Scheduled Jobs and Scheduled Tasks.

# Change these three variables to whatever you want
$jobname = "Recurring PowerShell Task"
$script =  "C:\Scripts\Test-ExampleScript.ps1 -Server server1"
$repeat = (New-TimeSpan -Minutes 5)

# The script below will run as the specified user (you will be prompted for credentials)
# and is set to be elevated to use the highest privileges.
# In addition, the task will run every 5 minutes or however long specified in $repeat.
$action = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument  "$script; quit"
$duration = ([timeSpan]::maxvalue)
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).Date -RepetitionInterval $repeat -RepetitionDuration $duration

$msg = "Enter the username and password that will run the task"; 
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)
$username = $credential.UserName
$password = $credential.GetNetworkCredential().Password
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd

Register-ScheduledTask -TaskName $jobname -Action $action -Trigger $trigger -RunLevel Highest -User $username -Password $password -Settings $settings

taskscheduler2
This job will appear under Task Scheduler -> Task Scheduler Library. Also, don’t forget to return $true or $false. If you don’t, the “Last Run Result” will be 0x1.

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in PowerShell, Windows