80

I have one PowerShell script which sends emails. I want to execute that script automatically, every 1 minute. How can I do it, using task scheduler?

Currently I have created a task and provided the path of my script. But that scheduler opens my script, instead of executing.

I am using Windows 7 Professional and PowerShell version 2.0.5.

TylerH
  • 20,799
  • 66
  • 75
  • 101
AK47
  • 3,707
  • 3
  • 17
  • 36

7 Answers7

123

Create the scheduled task and set the action to:

Program/Script: Powershell.exe

Arguments: -File "C:\Users\MyUser\Documents\ThisisMyFile.ps1"

Ian Gregory
  • 5,770
  • 1
  • 29
  • 42
Kevin_
  • 2,916
  • 3
  • 19
  • 18
  • 1
    Tried it, it says it ran the script, but in the script I have email sending and the email was never sent. When I run the script manually though the email is sent – John Demetriou Apr 28 '16 at 06:44
  • 2
    @JohnDemetriou normally this occurs because of what user account the Scheduled Task is executed as. – user4317867 Jun 18 '16 at 00:17
  • 1
    To further illustrate @user4317867's point, my powershell script to send an email would hang indefinitely in the `running` state when I set it to run as *the* domain admin. That is, `domain\Administrator` could not run the script, even if I left "run with the highest privileges" unchecked. However, my personal domain admin account could run the script just fine. Quite irritating as my user won't be with the company forever, but the domain admin user will never go away. – jdgregson Mar 07 '17 at 07:52
  • 4
    @jdgregson: Using a full admin account, especially a domain admin account, for anything that doesn't absolutely require it is not good practice security-wise, though nor is using using an account associated with a person as you correctly identify. The usual recommendation is to create a domain account specifically for such tasks that has only the privileges required for those tasks. This sometimes means having multiple maintenance accounts, each with different permissions. Obviously document this should an accident occur and someone in infrastructure needs to rebuild the arrangement. – David Spillett May 09 '17 at 09:25
  • Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe in case above is not working for you as this worked for me – 3rdi Nov 21 '21 at 14:43
14

Here is an example using PowerShell 3.0 or 4.0 for -RepeatIndefinitely and up:

# Trigger
$middayTrigger = New-JobTrigger -Daily -At "12:40 AM"
$midNightTrigger = New-JobTrigger -Daily -At "12:00 PM"
$atStartupeveryFiveMinutesTrigger = New-JobTrigger -once -At $(get-date) -RepetitionInterval $([timespan]::FromMinutes("1")) -RepeatIndefinitely

# Options
$option1 = New-ScheduledJobOption –StartIfIdle

$scriptPath1 = 'C:\Path and file name 1.PS1'
$scriptPath2 = "C:\Path and file name 2.PS1"

Register-ScheduledJob -Name ResetProdCache -FilePath $scriptPath1 -Trigger  $middayTrigger,$midNightTrigger -ScheduledJobOption $option1
Register-ScheduledJob -Name TestProdPing -FilePath $scriptPath2 -Trigger $atStartupeveryFiveMinutesTrigger
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • 1
    The `-RepeatIndefinitely` switch only exists in [PowerShell 4.0](http://technet.microsoft.com/en-us/library/hh849759(v=wps.630).aspx), not [PowerShell 3.0](http://technet.microsoft.com/en-us/library/hh849759(v=wps.620).aspx) – Chris May 30 '14 at 12:11
  • docs: https://learn.microsoft.com/en-us/powershell/module/psscheduledjob/register-scheduledjob?view=powershell-5.1#examples – Elliott Beach Apr 24 '19 at 20:16
12

Instead of only using the path to your script in the task scheduler, you should start PowerShell with your script in the task scheduler, e.g.

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NonInteractive -File "C:\Path\To\Your\PS1File.ps1"

See powershell /? for an explanation of those switches.

If you still get problems you should read this question.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ocaso Protal
  • 19,362
  • 8
  • 76
  • 83
7

After several hours of test and research over the Internet, I've finally found how to start my PowerShell script with task scheduler, thanks to the video Scheduling a PowerShell Script using Windows Task Scheduler by Jack Fruh @sharepointjack.

Program/script -> put full path through powershell.exe

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe

Add arguments -> Full path to the script, and the script, without any " ".

Start in (optional) -> The directory where your script resides, without any " ".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mickael Pilote
  • 170
  • 1
  • 4
7

In my case, my script has parameters, so I set:

Arguments: -Command "& C:\scripts\myscript.ps1 myParam1 myParam2"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Carlos Coelho
  • 566
  • 1
  • 5
  • 14
5

You can use the Unblock-File cmdlet to unblock the execution of this specific script. This prevents you doing any permanent policy changes which you may not want due to security concerns.

Unblock-File path_to_your_script

Source: Unblock-File

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AlexeyGy
  • 306
  • 3
  • 9
0

None of posted solutions worked for me. Workaround, which worked:

create a run.bat and put inside powershell.exe -file "C:\...\script.ps1"

then set Action to Program/Script: "C:\...\run.bat"

bucky
  • 392
  • 4
  • 18