Automating Scheduled Tasks with PowerShell

Automate Windows Task Scheduler with PowerShell using Register-ScheduledTask. Learn to create daily, weekly, and custom triggers with code examples. Configure SYSTEM accounts, troubleshoot failures, and deploy tasks across multiple EC2 instances.

2 min read
PowerShell Scheduler Automation

Scheduled Tasks are a cornerstone of automation on Windows servers—but configuring them manually through Task Scheduler is slow, error-prone, and completely unscalable.

Whether you want to run a nightly cleanup script, trigger a compliance check every hour, or reboot a rogue app every Sunday, PowerShell is your friend. In this post, we’ll show you how to create, configure, and troubleshoot scheduled tasks entirely from the command line.

Bonus: If you’re using Ohlala Operations, you can run these PowerShell commands across your EC2 instances directly—without RDP or GPOs.


Step 1: Define the Action

Every scheduled task needs an action. Here’s how to define one that runs PowerShell with a specific script:

$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Scripts\cleanup.ps1"

You can also run inline commands:

$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-Command `"Get-Process | Out-File C:\temp\procs.txt`""

Step 2: Define the Trigger

Want the task to run daily at 2 AM? Use a time-based trigger:

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

Need more control? You can create weekly, at-logon, or one-time triggers:

# Every Sunday at 3 AM
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 3am

Step 3: Set the Principal (Who Runs It)

By default, tasks run as the current user. But on a server, you typically want SYSTEM or a service account:

$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest

Replace "SYSTEM" with a domain user if needed.

Step 4: Register the Task

Now put it all together:

Register-ScheduledTask -TaskName "NightlyCleanup" `
    -Action $action `
    -Trigger $trigger `
    -Principal $principal `
    -Description "Deletes old temp files every night at 2 AM"

This task is now ready and visible under Task Scheduler > Task Scheduler Library.

Step 5: Verify and Troubleshoot

List all tasks:

Get-ScheduledTask

Check your specific task:

Get-ScheduledTask -TaskName "NightlyCleanup" | Get-ScheduledTaskInfo

Check the event log if it’s failing silently:

Get-WinEvent -LogName Microsoft-Windows-TaskScheduler/Operational | 
    Where-Object { $_.Message -like "*NightlyCleanup*" } |
    Select-Object TimeCreated, Id, Message -First 10

Bonus: Run This Remotely with Ohlala

Using Ohlala Operations, you can run all of the above PowerShell commands remotely—across one or many EC2 instances—without needing RDP or interactive logins.

Simply open the Command Runner in the Ohlala UI, paste the commands, and execute. For example:

$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Scripts\cleanup.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -TaskName "NightlyCleanup" -Action $action -Trigger $trigger -Principal $principal -Description "Deletes old temp files every night at 2 AM"

This gives you an easy way to deploy tasks across multiple servers from a single interface.

In the future, we’ll integrate scheduled task creation directly into Ohlala Recipes to make this even faster and more repeatable.

Wrap-Up

Creating scheduled tasks with PowerShell is simple, powerful, and automatable—which makes it a perfect fit for modern Windows server management. Skip the GUI, skip the clicks, and manage your tasks like code.

Have a task you want to automate across your Windows fleet? Try it with Ohlala Operations and see how zero-RDP server automation feels.

Ready to Automate Your EC2 Management?

Get 24/7 AI-powered infrastructure assistance in Microsoft Teams and Google Chat. SmartOps helps your team manage, monitor, and optimize EC2 instances for just $399/month.