My script is like-
$action = New-ScheduledTaskAction -Execute "BolusDemoDataGenerator.BAT"
$TaskStartTime = [datetime]::Now.AddMinutes(2)
$trigger = New-ScheduledTaskTrigger -At $TaskStartTime -Once
$setting = New-ScheduledTaskSettingSet
$inputTask = -action $action -trigger $trigger -settings
Register-ScheduledTask BatchRunTask -InputObject $inputTask
but I am getting an error
New-ScheduledTaskAction : The term 'New-ScheduledTaskAction' is not
recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a
path was included, verify that the path is
correct and try again.
At C:\Users\abc\Desktop\hi.ps1:1 char:11
+ $action = New-ScheduledTaskAction -Execute "BolusDemoDataGenerator.BAT"
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (New-ScheduledTaskAction:String)
[], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Not sure what is wrong I tried to execute this powershell script to create a task in task scheduler to execute a bat file present in desktop
With PowerShell 4.0 a scheduled task can be easily created with the new cmdlets New-ScheduledTaskAction
, New-ScheduledTaskTrigger
and Register-ScheduledTask
, but unfortunately it's not possible in the prévious versions (have a look to PSVersion
in $PSVersionTable
).
For previous version you can find many examples where people invoke the schtasks.exe
command.
If you want to avoid calling external executables and do as much in PowerShell as possible you can use the Task Scheduler’s com object for creating a scheduled task.
# Open the Task Scheduler com object
$service = new-object -ComObject("Schedule.Service")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With