Switch to the Triggers tab and click the “New…” button. Here you can specify the conditions that trigger the task to be executed. For example, you can have it executed on schedule, at logon, on idle, at startup or whenever a particular event occurs. We want our task to be triggered by any new software installation, so we choose “On an event” from the drop-down menu and select “Application” from the Log settings. Leave the “Source” parameter blank and set the EventID to “11707”. Click “OK” to save the changes.
To schedule the PowerShell script, specify the following parameters:
Click “OK” to save your changes.
Now whenever new software is installed on your Microsoft Windows Server, you will be notified via an email that details the time of the installation, the name of the software and the user ID (SID) of the person who installed it.
To modify an existing task, right-click it in the list, select Properties, edit the required settings and click OK. To delete a scheduled task, right-click it, select Delete and confirm the action.
Now that you know how to create a task using Task Scheduler, let’s find out how to create a scheduled task using PowerShell. Suppose we want our task to be launched daily at 10 AM, and it must execute the PowerShell script.
In Windows Powershell 2.0 (Windows 7, Windows Server 2008 R2), to create a scheduled job, you must use the TaskScheduler module. Install the module by running the “Import-Module TaskScheduler” command and use the following script to create a task that will execute the PowerShell script named GroupMembershipChanges.ps1 daily at 10 AM:
Import-Module TaskScheduler
$task = New-Task
$task.Settings.Hidden = $true
Add-TaskAction -Task $task -Path C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe –Arguments “-File C:\Scripts\Script.ps1”
Add-TaskTrigger -Task $task -Daily -At “10:00”
Register-ScheduledJob –Name ”Monitor Group Management” -Task $task
Windows PowerShell 4.0 (Windows Server 2012 R2 and above) doesn’t include the Task Scheduler module, so this script will not work. Instead, PowerShell 3.0 and 4.0 introduced new cmdlets for creating scheduled tasks, New-ScheduledTaskTrigger and Register-ScheduledTask, which make creating a scheduled task much easier and more convenient. So let’s create a task that will execute our script daily at 10 AM using the system account (SYSTEM). This task will be performed by an account with elevated privileges.
$Trigger= New-ScheduledTaskTrigger -At 10:00am –Daily # Specify the trigger settings
$User= "NT AUTHORITY\SYSTEM" # Specify the account to run the script
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "C:\PS\StartupScript.ps1" # Specify what program to run and with its parameters
Register-ScheduledTask -TaskName "MonitorGroupMembership" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force # Specify the name of the task
Other trigger options that could be useful in creating new tasks include:
Note that, using these cmdlets, it is not possible to trigger execution “on an event” as we did with the Task Scheduler tool. PowerShell scripts with “on an event” triggers are much more complicated, so this is a real disadvantage of using PowerShell rather than Task Scheduler.
As you can see, it is easy to create scheduled tasks using Task Scheduler or PowerShell. But remember that improper changes to your scheduled tasks can cause service interruptions and degrade server performance. Therefore, it’s essential to track all changes to your scheduled tasks