Run PowerShell Scripts from Task Scheduler

Powershell is now the defacto scripting language in Windows environments.  Microsoft have now decreed to all of it’s software units that they must create PowerShell cmdlet’s with full functionality to the software in all future releases.

To run Powershell scripts from the Windows Task Scheduler, follow the steps below:

1) Open Task Scheduler and Create a new task. Name it and set your security options:

2) Click on the Triggers tab and set your schedule or event that will trigger the running of your Powershell script.

3) Click on the Actions tab and click on New.

Action: Start a program

Program/script: Powershell.exe

You don’t need to put a path as it should already be on your system.

4) First you need to set the ExecutionPolicy. You have two options here, you can set the ExecutionPolicy on the machine or you can do it on a per-script basis. Read the PowerShell ExecutionPolicy link below as it talks about or you can issue the command:

Get-Help About_execution_policies

To set the execution policy globally, you can issue this command from within PowerShell:

Set-ExecutionPolicy Unrestricted

Or use one of the other settings available depending on your environment. In the context of this how-to, however, we want to set the execution policy on a per script basis and open up security for us to run the script. This security policy will only be in effect for the script we are running and not compromise security otherwise.

That means we use the following Argument:

-ExecutionPolicy Bypass

5)  Next comes the path and name of your script. This can be either a drive letter path, or a full UNC. I’ve run into problems using the -FILE parameter so I don’t use it.

c:\scripts\myscript.ps1

6)  If your script has any parameters, you want to add them now:

-RunType $true -Path c:\mydatafiles

Please keep in mind these arguments/parameters are purely as an example, many scripts won’t have any arguments at all.

7) That makes your full argument line look like so:

-ExecutionPolicy Bypass c:\scripts\myscript.ps1 -RunType $true -Path c:\mydatafiles

8) Save your scheduled task and run it.

I’ve found it helpful to run the task with the “Run only when user is logged on” security option turned on (on the General tab). This will run your script interactively and will allow you to observe it and see if there are any errors.

Once you have a clean run you can change the security option back to “Run whether user is logged on or not”.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.