Skip to main content

Sometimes you want to configure a scheduled task for a PowerShell script. Let the PowerShell script run automatically in the background every hour or day. In our example, we like to create a scheduled task for the Health Checker PowerShell script. Suppose you want to create a scheduled task for another PowerShell script; the same steps will apply. In this article, you will learn how to create a scheduled task with PowerShell.

Before you start to create a scheduled task

Prepare the PowerShell script on the machine and create a service account with minimum privileges to run the script before you start creating a scheduled task.

Add script to folder

Create a Scripts folder on the (C:) drive. Place the PowerShell script in the C:\scripts folder.

In our example, it’s the PowerShell script HealthChecker.ps1.

Create scheduled task with PowerShell scripts folder

Create service account

It’s essential to create a service account with a strong password that will run the script.

In this example, we did create the service account svc-hc.

Create scheduled task with PowerShell create service account

Add the user to the View-Only Organization Management.

Create scheduled task with PowerShell add view-only organization management

Add service account to local administrators group

Add the service account to the local administrators group on each server that you want to run the PowerShell script against by following the below steps:

  • Start Computer Management
  • Click on Groups
  • Double-click Administrators
  • Add the service account svc-hc
Create scheduled task with PowerShell administrators group

If you don’t add the service account to the local administrators group, the PowerShell script will not run.

How to create scheduled task with Powershell

To create a scheduled task with PowerShell, we will use the below cmdlets:

  1. New-ScheduledTaskTrigger
  2. New-ScheduledTaskSettingsSet
  3. New-ScheduledTaskAction
  4. New-ScheduledTask
  5. Register-ScheduledTask

Note: Start Windows PowerShell as administrator and run the below commands.

Step 1. Create a scheduled task trigger object

Create a trigger that defines when the script should be executed.

Example 1. Daily at 2 AM.

$hcTrigger = New-ScheduledTaskTrigger -Daily -At 2am

Example 2. Every four weeks on Monday at 2 AM.

$hcTrigger = New-ScheduledTaskTrigger -Weekly -WeeksInterval 4 -DaysOfWeek Monday -At 2am

Step 2. Create new scheduled task settings object

Create a new scheduled task settings object using the default settings.

$hcSettings = New-ScheduledTaskSettingsSet

Step 3. Create scheduled task action

Creates a scheduled task action and define the actions that need to execute.

Example 1. Update the HealthChecker script, execute the script against the local server and generate the HTML report:

$hcAction = New-ScheduledTaskAction -Execute 'powershell.exe' -WorkingDirectory "C:\Scripts\" -Argument '-NonInteractive -NoLogo -NoProfile -Command ".\HealthChecker.ps1 -ScriptUpdateOnly; .\HealthChecker.ps1; .\HealthChecker.ps1 -BuildHtmlServersReport"'

Example 2. Run the HealthChecker script against a remote Exchange server EX02-2019.

$hcAction = New-ScheduledTaskAction -Execute 'powershell.exe' -WorkingDirectory "C:\Scripts\" -Argument '-NonInteractive -NoLogo -NoProfile -Command ".\HealthChecker.ps1 -ScriptUpdateOnly; .\HealthChecker.ps1 -Server EX02-2019; .\HealthChecker.ps1 -BuildHtmlServersReport"'

Step 4. Create scheduled task instance

Create a new scheduled task object using the pre-defined action, trigger, and settings objects:

$hcTask = New-ScheduledTask -Action $hcAction -Trigger $hcTrigger -Settings $hcSettings

Step 5. Register scheduled task definition on local computer

Create the scheduled task.

Register-ScheduledTask -TaskName 'HealthChecker Daily Run' -InputObject $hcTask -User (Read-Host "Please enter username in format (Domain\Username)") -Password (Read-Host "Please enter password")

The output shows the task name.

TaskPath    TaskName                   State
--------    --------                   -----
\           HealthChecker Daily Run    Ready

Run scheduled task in Task Scheduler

Always manually run a scheduled task to test that everything works:

  • Start Task Scheduler
  • Right-click the task
  • Click on Run
Create scheduled task with PowerShell run script

After the script finishes, it shows the last run result: The operation completed successfully. (0x0)

Create scheduled task with PowerShell operation completed

Verify PowerShell script report

Check that the PowerShell script successfully did export the results.

Verify script report

Leave a Reply