Scheduled task is one of the useful feature in Windows Operating System enable to schedule to launch a program or to run some scripts at specified time, date or even when particular event occurred.
PowerShell provides commands to create scheduled tasks. In this Article, I am going to explain, what are the commands in PowerShell useful to create the scheduled tasks.
Before we start creating the scheduled tasks; lets’ verify whether ScheduledTask module is exists in your System. If this module doesn’t exist; you can run these commands in PowerShell.
Lets’ run below command at PowerShell prompt. You may see below message; that means, related ScheduledTask module doesn’t exist in your System. I have tested this on Windows 8 (64-bit) system; and it worked fine. But it doesn’t on Windows 7 (64-bit) system.
PS C:\> Get-ScheduledTask Get-ScheduledTask : The term 'Get-ScheduledTask' 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 line:1 char:1 + Get-ScheduledTask + ~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-ScheduledTask:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
Display list of scheduled tasks
Get-ScheduledTask
cmdlet is useful to provide list of scheduled tasks which are registered in the local system. Remember this work, registered. Below is the command which returns list of scheduled tasks.
PS C:\> Get-ScheduledTask TaskPath TaskName State -------- -------- ----- \Microsoft\Windows\.NET Framework\ .NET Framework NGEN v4.0.30319 Ready \Microsoft\Windows\.NET Framework\ .NET Framework NGEN v4.0.30319... Disabled .......... .......... \Microsoft\Windows\WS\ WSTask Ready PS C:\>
We can filter the above command to display; only, currently Running scheduled tasks.
Create Scheduled Task
New-ScheduledTask
cmdlet is used to create a scheduled task. When you create a scheduled task; you need to specify what Action to perform when you run the task.
Lets’ try to create a task using this below command.
PS C:\> New-ScheduledTask -Action "notepad.exe"
New-ScheduledTask : Cannot process argument transformation on parameter 'Action'. Cannot convert value "notepad.exe" to type
"Microsoft.Management.Infrastructure.CimInstance[]". Error: "Cannot convert value "notepad.exe" to type "Microsoft.Management.Infrastructure.CimInstance".
Error: "Specified argument was out of the range of valid values.
Parameter name: className""
At line:1 char:27
+ New-ScheduledTask -Action "notepad.exe"
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-ScheduledTask], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,New-ScheduledTask
PS C:\>
Notice that, PowerShell throws the error, when we try to create a scheduled task, using above command. Why? New-ScheduledTask
cmdlet expects an object of CimInstance, not the string as an Action. How do we get this object? PowerShell provides another cmdlet, New-ScheduledTaskAction
, to create an object that contains a definition of scheduled task action. But how to pass this to New-ScheduledTask
? Simple. Store this object into a variable.
Lets’ create a task:
PS C:\> $Action = New-ScheduledTaskAction -Execute "notepad.exe" PS C:\> New-ScheduledTask -Action $Action -Description "notepad.exe" TaskPath TaskName State -------- -------- ----- PS C:\>
Have you seen, the created Scheduled task? No. Why? Once the scheduled task is created, it must be registered (I have mentioned this word above; and ask to remember this); to add to the Task Scheduler. Then it will appear in the list when you run the command Get-ScheduledTask
. Below section explains how to register a scheduled task.
Register a Scheduled Task
Register-ScheduledTask
cmdlet is meant for register the scheduled task into Windows Task Scheduler. We have created the scheduled task object, through above command; we must pass that object to this cmdlet; to register the scheduled task. How to pass scheduled task object? This cmdlet has, InputObject parameter from where it takes the object as input. What we do is, assign out task object to a variable and pass it to InputObject parameter. Below statements, register a scheduled task to Task Scheduler.
PS C:\> $Task = New-ScheduledTask -Action $Action -Description "notepad.exe" PS C:\> Register-ScheduledTask -TaskName "notepad.exe" -InputObject $Task TaskPath TaskName State -------- -------- ----- \ notepad.exe Ready PS C:\>
Observer that, once the task is created & registered; it shows the task details. Run the command, Get-ScheduledTask
to see the newly created scheduled task.
These are the commands used to create the scheduled tasks using PowerShell. Note that, you must have ScheduledTasks module available in your system to use these commands.
We will discuss more scheduled task command in PowerShell; through my upcoming Articles.
[..] David
2 thoughts on “PowerShell – Create Scheduled Task”