skip to Main Content

Create scheduled task with PowerShell

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

Read more: Exchange Server health check with PowerShell script »

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

Did this help you to schedule a task with PowerShell on Windows?

Read more: Clear Windows Defender Antivirus exclusions with PowerShell »

Conclusion

You learned how to create a scheduled task with PowerShell. First, create a service account and make it a member of the view-only organization management group. The next step is to add the service account to the local administrator group. As of last, run the commands to create a new scheduled task. Don’t forget to manually run the scheduled task and verify your work.

Did you enjoy this article? You may also like Get Active Directory count with PowerShell. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 0 Comments

Leave a Reply

Your email address will not be published.