TechShizz | How to run a Power Shell script from Task Scheduler

How to run a Power Shell script from Task Scheduler

Problem

You are unable to get a Power Shell file to execute using the Task Scheduler

Cause

Two reasons:

  1. The execution policy needs to be set to Unrestricted
  2. The command in Task Scheduler needs to adhere to the correct syntax
  3. You must store the script in a place PowerShell trusts.
Solution

Firstly, store your powershell script in a place PowerShell trusts. To find this, open PowerShell and run:

$env:path

You will note that one of the safe places is "C:\Windows\System32".

Next, Set the execution Policy to Unrestricted using PowerShell. Open PowerShell as administrator and run:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Press "A" to accept the changes. If you are using a computer account to run the script, you may need to set the execution policy on the local machine to Unrestricted like this:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine

Create a sheduled task as desired. On the actions tab the script must be run like this:

PowerShell.exe "C:\Windows\System32\MyScript.ps1"

Taking care to wrap the path in double quotes. 

Add comment