How to Automate PowerShell Scripts with Task Scheduler

Running PowerShell scripts on a schedule is a common requirement for Windows Admins. This guide will show you how to automate your scripts using Task Scheduler, covering both the GUI and PowerShell methods.

Task Scheduler allows you to run programs, scripts, or other tasks based on a schedule you define. Whether you’re an administrator scheduling system tasks or automating workflows, this is an essential thing to know about.

As a SQL DBA, we’d generally automate tasks, including PowerShell scripts, using the SQL Server Agent. If working with SQL Server Web Edition, you do not have the luxury of having a SQL Agent and would need to schedule tasks the way we describe here.

This post covers the following :
> Create a Scheduled Task using PowerShell
> Create a Scheduled Task for a PowerShell Script (GUI Option)
> More Tips

Create Scheduled Task using PowerShell

1. Create Your PowerShell Script
Write the PowerShell script you want to schedule. For this example, the script logs the system’s average CPU usage with a timestamp into a text file. Save your script, e.g., avg_cpu_collector.ps1, in C:\temp\PowerShell_Scripts.

Create PowerShell Script

2. Create the Scheduled Task
Use the following PowerShell code to create a scheduled task that runs your script daily at 8:05 AM:

$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

3. Verify the Task
Open Task Scheduler to confirm the task was created under the specified path (\Admin\).

    PowerShell Create New Scheduled Task

    Create a Scheduled Task for a PowerShell Script (GUI)

    1. Open Task Scheduler
    Open the Task Scheduler application from the Start Menu.

    2. Create a New Task
    > Right-click in the empty area and select Create Task.
    > In the General tab, enter a name (e.g., DailyPSCollector).

    Task Scheduler Create New Task

    3. Set the Trigger
    > Choose Daily and set the time to 8:05 AM (or your preferred schedule).
    > Go to the Triggers tab and click New.

    Task Scheduler Triggers Tab

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

    Task Scheduler Actions Tab

    5. Review the Settings
    > Click OK to save.
    > Check the Settings tab for additional options like allowing the task to run on demand.

    Task Scheduler Settings Tab

    6. Verify Your Task
    You’ll now see the task listed in the Task Scheduler main window.

    Task Scheduler PowerShell Script

    More Tips for Automating PowerShell Scripts

    > Use Proper Permissions: Run your scheduled tasks with a user account that has the necessary permissions.
    > Test Your Script First: Before scheduling, run your PowerShell script manually to ensure it works as expected.
    > Consider Logging: Add logging to your script to track its execution and troubleshoot any issues.
    > Backup Your Tasks: Use PowerShell to export your scheduled tasks for backup or migration.


    Comments

    Leave a Reply

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