I'm trying to add a Powershell script to task scheduler to run on a regular basis.

Import-Module ScheduledTasks

$scriptFilePath = "C:\hello-there.ps1"
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File $scriptFilePath -Noninteractive"

$repetitionDuration = New-TimeSpan -End (Get-Date -Year 2030 -Month 4)
$repetitionInterval = New-TimeSpan -Minute 15

$trigger = New-ScheduledTaskTrigger -RepetitionDuration $repetitionDuration -RepetitionInterval $repetitionInterval
Register-ScheduledTask -TaskName "custom-task" -Description "Does something important" -Trigger $trigger -Action $action

It executes until New-ScheduledTaskTrigger line, then asks me for user input? "Supply values for the following parameters"
What am I doing wrong?

  • You aren't specifying enough parameters for New-ScheduledTaskTrigger. The host is asking you to supply values for the missing mandatory parameters. – Mike Shepard Apr 23 '18 at 14:28
  • @MikeShepard which ones? I read documentation - At, Daily, AtStartup, AtLogon are use-case specific – chester89 Apr 23 '18 at 14:32

Looking at the help for New-ScheduledTaskTrigger (for the ParameterSet which includes the 2 -Repetition* parameters you have specified) you can see that -At is not in brackets, so it's mandatory.

   [-RandomDelay <TimeSpan>]
   -At <DateTime>
   [-RepetitionDuration <TimeSpan>]
   [-RepetitionInterval <TimeSpan>]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]

It turns out you need to specify -Once -At (Get-Date) to make it work (I found a clue in an answer here)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.