How To Automate PowerShell Scripts With Windows Task Scheduler

Automate the execution of PowerShell scripts using Windows Task Scheduler. Learn this trick for simplifying your workflow.

Brien Posey

July 27, 2023

4 Min Read
glowing gears in a digital network
Alamy

Windows PowerShell scripts can be used to automate all manner of tasks within a Windows environment. However, for these automations to be truly useful, you may need to schedule them.

In my own organization, for example, I use PowerShell scripts to monitor storage array health, Hyper-V replication, and more. I also use a script that creates file-level backups of my unstructured data. While these scripts can be manually executed, it is far more practical to run them on a scheduled basis. That way, you don’t need to remember to run them.

Steps for Using Windows Task Scheduler

The Windows Task Scheduler can schedule the execution of PowerShell scripts at specified times. Although the process of scheduling a PowerShell script isn’t overly complex, it may not be nearly as intuitive as you might assume. To illustrate this, let’s look at the steps for in creating a scheduled task that is not directly related to PowerShell.

To schedule a task using Task Schedule, follow these steps:

  1. Open Task Scheduler and right-click on Task Scheduler Library. From the shortcut menu, select “Create Basic Task” to launch the Create Basic Task Wizard.

  2. On the initial screen of the wizard, enter a name and a description for the task you are creating. Having meaningful names and descriptions is helpful when managing multiple tasks over time.

  3. Click Next to go to a screen that asks you when you want the task to start. As you can see in Figure 1, you can choose from options like daily, weekly, monthly, at computer startup, when a specific event is logged, and when you log in. You can also opt to run the task only once.

Related:Debugging Scripts in PowerShell: Breakpoints and Stepping

Windows Task Schedule screenshot shows the Task Trigger options

Task Scheduler 1

Figure 1. Choose when you want to run the task.

  1. After making your selection, click Next. You will go to a screen where you can specify the schedule for the task to occur. The options on this screen may vary based on your previous selection, but typically, you will enter a start date and time for the task.

  2. When you are done setting the schedule, click Next again. You will see a screen that asks what should happen at the scheduled time. For example, you can start a program, send an email, or display a message. Since we are discussing PowerShell in this case, choose the “Start a Program” option and click Next.

  3. On the next screen, you will see a field like the one shown in Figure 2. Here, you should enter the path and filename of the program that you want to launch (in this case, the PowerShell executable), along with any necessary command line arguments. Once you have provided this information, click Next, and then Finish to create the scheduled task.

Related:Use AppLocker to Gain Control Over Desktop Applications

Windows Task Scheduler screenshot shows Start a Program screen

Task Scheduler 2

Figure 2. Enter the path to the executable that you want to run.

Executing PowerShell Scripts With Task Scheduler

As straightforward as creating a scheduled task may seem, you can’t simply enter the path and filename of a PowerShell script directly. If you do so, the task will likely open the script for editing (depending on your computer’s configuration) and won’t execute the script.

This happens because Task Scheduler operates similarly to the Windows Run prompt. Since the Windows Run prompt links to the Windows Command environment, not to PowerShell, you can’t just enter the name of a PowerShell script into the Run prompt and expect it to execute.

The trick to solving this problem is to use the "Start a Program" interface shown in the figure above to launch PowerShell itself, not your PowerShell script. As you may recall in Step 6, this interface allows you to enter command line arguments, and, fortunately, PowerShell.exe just happens to accept a particular argument called File. Using the File argument, you can specify the path and filename of the PowerShell script you want to execute. Additionally, if you want the script to remain visible on screen while running, you can also use the NoExit argument. You can see an example of what this looks like in Figure 3.

Windows Task Scheduler screenshot

Task Scheduler 3

Figure 3. This is how you schedule a PowerShell script to run automatically.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

https://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like