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:

** Please note: I am assuming you already know how to schedule a task and don’t need detailed instructions for that.

Step 1: Open Task Scheduler

36cad5283fcfcf840b353680676936550349eb4d68470f275925743e337c0c6c_createtask.jpg

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

Step 2: Set Triggers

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

Step 3: Create your Action

8519a2f519b07af55c2de43af78056169599e68ae99fa824792803483bf79d8d_NewAction.jpg

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.

Step 4: 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

Step 5: Set the next argument

bf8c65c134cd5b4a63a973530d14b32246a6dae40e72b0b0eb49d1e3a84b1acd_script.jpg

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

Step 6: 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.

Step 7: Full Argument

That makes your full argument line look like so:

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

Step 8: Save the scheduled task

4dc8c9683dc9f0784f877b3a4abcd3cd087001a902f602be5f841f8c89e2ced1_thetask.jpg

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

193 Spice ups

Thank you! This has been why I always revert to batch files.

Nicely done.

I liked this one: Archived MSDN and TechNet Blogs | Microsoft Learn

Thanks martin - Well done - This was my first PowerShell script which worked great but couldn’t get it to run in Task scheduler.

Thanks martin - Well done - This was my first PowerShell script which worked great but couldn’t get it to run in Task scheduler.

Just used this to set up a task! Thanks a lot!

Hi,
I am unable to execute the script using the task scheduler. Tried all means but have not been successful. The history shows that the task has been run successfully but with return code 1
Task Scheduler successfully completed task “\Backup Unsealed Management Pack” , instance “{xxxxxxxxxxxxxxxxxxxxxxxxxxxx}” , action “Powershell.exe” with return code 1.
Kindly assist

Jesty, your best bet is to post in the PowerShell forum and include your execution page and the full error message.

Thanks this came in handy to run a powershell script to cleanup old IIS & Reporting Services logs :slight_smile:

If you run 64bit OS, Make sure when setting up the task that you point to the 32bit exe for powershell. Just typing powershell.exe will launch 64bit and fail with all kinds of errors

Thanks! Step 7 was what I was missing. “-ExecutionPolicy Bypass” ended up being exactly the argument to get my PowerShell script to run on a 2012 server.

Thank you for this!!! I finally can get a client off my back about server, process, and services status!!!

Very nice!

I wish I have found this before I spent about 30 minutes figuring this out.

Thanks Martin. Just starting off in PowerShell, the help you’ve provided in the forums and with things like this “How-To” has been a great help!

Thanks Martin,
for exchange servers you need more argumnts
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2.0 -NonInteractive -WindowStyle Hidden -command “& ‘yourscript.ps1’ -MonitoringContext -ShowDetailedErrors -ErrorAction:Continue”

If your PS sends email, you need to configure McAfee

Thanks! That was exactly what I needed for my project.

Worked a treat! I’ve been trying to get task scheduler to run this PS script for ages, now I dont have to worry :slight_smile: Thanks

Just what I needed to get my script running. Thank you.