Most we will use the graphic interface of Taskschd.msc console to create Windows Task Scheduler jobs and Building a single scheduled task via the GUI task scheduler might not be a big deal.ย But if you find yourself creating scheduled tasks repeatedly it might be a good idea to use a method that scales better than a GUI like PowerShell.ย However, in various scripts and automated jobs, it is much more convenient to use the PowerShell features to create scheduled tasks. In this article, we will discuss about how to create new Windows Scheduler tasks using PowerShell.
PowerShell v4 introduced a ScheduledTasks module that greatly simplified the times of creating Task Scheduler COM objects to build scheduled tasks from the command line. To create Task Scheduler, we can utlize the below setting.
Example: Passing PS file as argument
1 2 3 4 5 6 |
# Specify the trigger settings $trigger= New-ScheduledTaskTrigger -At 10:00am โDaily # Specify what program to run and with its parameter $action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "C:\dotnet-helpers\TestScript.ps1" # Specify the name of the task Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "EventLog" -Description "Application Logs" |
Example: Passing scrip as argument
1 2 3 4 |
# In argument section - The script will get the evetlog and save in the excel in specific location. $action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -command "& {get-eventlog -logname Application -After ((get-date).AddDays(-2)) | Export-Csv -Path c:\dotnet-helpers\eventlog.csv -Force -NoTypeInformation}"' $trigger = New-ScheduledTaskTrigger -Daily -At 7:41PM Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "EventLog" -Description "Application Logs" |
OUTPUT
Explanation
New-ScheduledTaskAction
The New-ScheduledTaskAction cmdlet creates an object that contains the definition of a scheduled task action.ย A scheduled task action represents a command that a task executes when Task Scheduler runs the task.
New-ScheduledTaskTrigger
The New-ScheduledTaskTrigger cmdlet creates and returns a new scheduled task trigger object.
New-ScheduledTaskPrincipal
Creates an object that contains a scheduled task principal. Using this we can registers a scheduled task that will run as the Local Service account orย scheduled task that runs under logged-in members of the Administrators user group that has the highest privileges.
New-ScheduledTaskSettingsSet
The New-ScheduledTaskSettingsSet cmdlet creates an object that contains scheduled task settings. Each scheduled task has one set of task settings.
New-ScheduledTask
The New-ScheduledTask cmdlet creates an object that contains the definition of a scheduled task. New-ScheduledTask does not automatically register the object with the Task Scheduler service.
Register-ScheduledTask
The Register-ScheduledTask cmdlet registers a scheduled task definition on a local computer.
What do you think?
I hope you have idea of how to create Task in Window task scheduler using Powershell . I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.
Leave A Comment