How to Automate PowerShell Scripts with Task Scheduler

This post is useful if you need to run PowerShell Scripts automatically with a set schedule in Windows. In the demos below, we’re creating new Scheduled Tasks via GUI and with PowerShell.

Task Scheduler is a Windows application that we can use to create and schedule any task. We can set our Task Actions to start programs, send emails, and display messages, or we can run any script – likely a PowerShell script since we’re on Windows. These Actions can be triggered on any schedule you configure your Scheduled Task to use.

As a Systems Administrator, adding a Scheduled Task in Windows is something we might need to do regularly. As a SQL Database Administrator, we’d use the SQL Server Agent to schedule PowerShell scripts and other tasks. And Linux Sysadmins, they’ll use Cron.

This post covers the following :
# How to Create a Scheduled Task using PowerShell (CLI Option)
# How to Create a Scheduled Task for a PowerShell Script (GUI Option)

How to Create Scheduled Task using PowerShell

First, we need to create the PowerShell script that we want to be executed o a schedule. In the screenshot below, I’m creating an example .ps1 script in my c:\temp directory for this demo.

The script below updates the content of a text file, logging the current Average CPU of the computer with date/time. You could have anything in your PowerShell script, e.g. Restart-Service *ServiceName* or Restart-Computer.

Create PowerShell Script

Now, I’m creating a new Scheduled Task that will run the above PowerShell script on a daily Schedule. The following script uses the New-ScheduledTask and Register-ScheduledTask cmdlets.

$actions = (New-ScheduledTaskAction -Execute 'C:\temp\PowerShell_Scripts\avg_cpu_collector.ps1')
$principal = New-ScheduledTaskPrincipal -UserId 'Administrator' -RunLevel Highest
$trigger = New-ScheduledTaskTrigger -Daily -At '8:05 AM'
$settings = New-ScheduledTaskSettingsSet -WakeToRun
$task = New-ScheduledTask -Action $actions -Principal $principal -Trigger $trigger -Settings $settings
$taskPath = '\Admin\'
# create new scheduled task as per parameters above
Register-ScheduledTask 'DailyPSCollector' -InputObject $task -TaskPath $taskPath
PowerShell Create New Scheduled Task

How to Create a Scheduled Task for a PowerShell Script (GUI)

This is the GUI way of creating Scheduled Tasks in Windows. Open the Task Scheduler application to get started, and right-click in the empty area as shown to Create New Task.

Populate details in the General tab as shown in the screenshot below.

Task Scheduler Create New Task

Go to the Triggers tab to configure the desired schedule for our new job.

Task Scheduler Triggers Tab

Next, in the Actions Tab click to create a new task:

Action: Start a program
Program/script: powershell
Arguments: -File “C:\temp\PowerShell_Scripts\avg_cpu_collector.ps1”

Task Scheduler Actions Tab

Have a quick review of the Settings tab and click OK once done.

Task Scheduler Settings Tab

We can now review our new task in the Task Scheduler main window.

Task Scheduler PowerShell Script

And that’s us sorted. The PowerShell script will run daily at 6 pm as we specified during Task creation.

Feel free to check out the Windows Admin and/or PowerShell Tips tags for more random informational blog posts.


Comments

One response to “How to Automate PowerShell Scripts with Task Scheduler”

  1. […] post covers the following :# Create a Scheduled Task using PowerShell (CLI Option)# Create a Scheduled Task for a PowerShell Script (GUI […]

Leave a Reply

Your email address will not be published. Required fields are marked *