How to run powershell script using task scheduler

By FoxLearn 10/1/2024 9:31:27 AM   24
Running a PowerShell script using Task Scheduler in Windows is straightforward.

Windows Task Scheduler allows users to schedule tasks to run at specific times, on set schedules, or in response to certain events. This built-in tool enhances efficiency and ensures the reliable execution of repetitive tasks.

In this blog, we will explain how to run a PowerShell script from Task Scheduler and how to create scheduled tasks using PowerShell.

How to Automate PowerShell Scripts with Task Scheduler

You can use any text editor to create your PowerShell script (e.g., script.ps1) and save it in a known location (e.g., C:\Scripts\script.ps1).

For example, here’s a simple script that outputs "Hello, World!" to the console:

# HelloWorld.ps1
Write-Host "Hello, World!"

Next, Press Windows + R, type taskschd.msc, and press Enter to open Task Scheduler.

run powershell script on task scheduler

In the right pane, click on "Create Basic Task..." or "Create Task..." for more options.

create a basic task

Give your task a name and description, then click Next.

create a basic task

Choose when you want the task to start (e.g., Daily, Weekly, One time, etc.), then click Next.

create a basic task

Configure the trigger details (e.g., time and frequency), and click Next.

create a basic task

Select "Start a program" and click Next.

create a basic task

In the Program/script field, enter powershell.exe.

In the Add arguments (optional) field, enter the following

-ExecutionPolicy Bypass -File "C:\Scripts\script.ps1"

If you want your PowerShell script to remain visible on the screen while it runs, you can use the -NoExit argument. This keeps the PowerShell window open after the script finishes executing.

-NoExit -File "C:\Scripts\script.ps1"

Make sure to replace the path with the actual path of your script.

Click Next to review the settings.

create a basic task

Click Finish to create the task.

To test it, you can right-click the task and select Run.