How to run a PowerShell Script with Task scheduler

PowerShell scripts it's one of the best options to automate lot of small tasks that should do every day. But how to run a PowerShell script on specific time? This is a job for the the Windows Task Scheduler. 

Today i will explain how to create schedule a task that can run a powershell script. 

Let's see it in practice.

 

How to create a schedule task to run the PowerShell script.

As an IT Admin or System Engineer you will have create lot of schedule tasks. However when you should schedule a task with a PowerShell script should follow different steps as usual.

#Step 1 Open Task Scheduler 

  • Right click in start button
  • Click on Computer Management
  • Expand the Task Scheduler
Open the Task Scheduler

 

#Step 2 Create the Task

  • Right click on Task Scheduler and select Create a Task
Create a Task in Task Scheduler

 

  • Type a Name that you would like to give.

 

  • Click on Tab Actions.
  • Click on Create New
Create a Task in Task Scheduler

 

  • Leave the Action in Start a Program
  • In the Program/script the only that you need to do is to type powershell
  • In the Add arguments (optional) we should type first -ExecutionPolicy Bypass and then the full path which the powershell script is located.
  • We have used the Execution Policy Bypass because the default PowerShell Execution Policy is Restricted. When we add the PowerShell script in task scheduler  we don't have another way to change the Execution Policy.
  • The Argument will be as follow
    -ExecutionPolicy Bypass -File <full path of the file>
  • Click OK.

 

  • Go to the Tab Triggers to schedule the task.
  • Click New.
  • For the purposes of the Article I used the One time and I gave the specific date and hour. 
  • However, you should use this settings based on your requirements.
  • Click OK and OK.
Schedule the task in Task Scheduler

 

  • Go in Tab General and check the option Run whether user is logged or not. Sometimes should be use the Run with highest privileges as well.
  • If you don't check the Run whether user is logged or not then the script will not run if you are not log in with the user that you have create the Task Scheduler.
  • Click OK , type your credentials and it's ready.
  • I recommend to run the task once manually to verify that it's working as expected.
Create a Task in Task Scheduler

 

 

How to use PowerShell to create the Task Scheduler.

We can of course create the Task scheduler with a few PowerShell commands.

For every step we need a different PowerShell command. So let's take a look the PowerShell commands that will be use

  1. New-ScheduledTaskAction = Create the Action of the Schedule Task
  2. New-ScheduledTaskTrigger = Create the Trigger of the Schedule Task
  3. Register-ScheduledTask = Register the Schedule Task

So let's run one by one the commands 

  • Open the PowerShell as Administrator
  • Create the Action of the Task Scheduler with the following command and save it in a variable $action.
    $action=New-ScheduledTaskAction -Execute powershell -Argument "-Executionpolicy Bypass -File 'C:\srask\test-script.ps1'"

 

  • Create the Trigger of the Task Scheduler with the following command and save it in a variable $trigger.
    New-ScheduledTaskTrigger -Once -At "2:56 AM"

 

  • Create the Task with the following command.
    Register-ScheduledTask -TaskName 'RunPSScript' -Action $action -Trigger $trigger -User 'askme4tech\administrator' -Password 'Mytest1234'

 

  • If you go in GUI of the Task Scheduler you will see the new task.

 

Creating a Task in Task Scheduler can be done with both options from PowerShell or GUI. Depends of what it likes to everyone can use one or the other option to schedule a PowerShell script with the Task scheduler.

I hope to learn something today. 

Have a nice day !!

Tags