Run Powershell script from Scheduled Task

    Powershell is a very powerful automation engine that lets us do a lot of tasks that otherwise would take a long time. Sometimes it is also needed to do a task without admin interaction; maybe it is a recurrent task or a one time job done at a not so common working hour. In this case running a Powershell script from Scheduled Task comes in handy.

    This tip has been tested on Windows Server 2008 R2/Windows 7 up to Windows Server 2016/Windows 10 and it is working 100% on all operating systems.

    Set up task to run Powershell script

    First let’s write a smal script to test this action. In my example I am getting a list of processes and putting the text in a file. Just create a new text file, write the command below and save it as script.ps1 for example (the .ps1 is important). I put the script in a folder: C:\Task


Get-Process | Out-File C:\Task\out.txt

    Now just create a new Basic Task and set a cool name like PS Task:

Basic task to run a powershell script

Basic task to run a powershell script

    The schedule is not important because we will test the task manually anyway. Select “Start a program” as the action the task performs.

The action for the task

The action for the task

    Now comes the most important part. Let’s set the task to run our script. In the program field type powershell.exe. The arguments field should contain a minimum of 2 parameters: the ExecutionPolicy argument which is Bypass and File which is the path to the script; in my case it is C:\Task\script.ps1.


-ExecutionPolicy Bypass -File C:\Task\script.ps1

Code to run the powershell script from a scheduled task

Code to run the powershell script from a scheduled task

    This is all. Just finish the wizard and the task is ready to be run.

    Test the task

    To see if it is working we just need to run the task and check in the C:\Task folder if the out.txt file is created.

    And here it is:

Result of the powershell script

Result of the powershell script

 

 

Leave a Comment

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