User Tools

Site Tools


Sidebar

microsoft:powershell_script_scheduler

PowerShell Scripts from Task Scheduler

PowerShell is quickly taking over as the defacto scripting language in Windows environments–in fact, Microsoft has 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.

That means we have to learn how to run scripts from the Task Scheduler. Follow the steps below to do this:

I am assuming you already know how to schedule a task and don't need detailed instructions for that.

Open Task Scheduler

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

Set Triggers

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

Create your Action

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.

Set Argument

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

Set the next argument

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

Add parameters

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.

Full Argument

That makes your full argument line look like so:

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

Save the scheduled task

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”.

microsoft/powershell_script_scheduler.txt · Last modified: 2019/02/26 14:43 by admin