With the help of Group Policy, the schtasks command, and PowerShell, you can run scheduled tasks on multiple computers. In this post, I provide an overview of those three options.

One could say that task scheduling is a self-explanatory topic. Well, it probably is if we are talking only about a GUI. But a GUI is only good if we need to create the task for one or two computers. If we need to run the task on tens or hundreds of computers, going to each of them to do it manually is probably not a good idea.

Group Policy

The easiest way is probably to use Group Policy. The only condition: you need to have an Active Directory domain to do that. If you do have it, the rest is as simple as creating the task for one computer. You just create a Group Policy object or use an existing one and go to Computer Configuration -> Preferences -> Control Panel -> Scheduled tasks. Then click “New,” and you are dealing with an interface that is pretty much the same as one you are used to create a task on a standalone machine. The only difference is that you can also change, remove, and update existing tasks with the same name. Just take a look at the screenshot below:

Group policy scheduled task interface

Group policy scheduled task interface

Then you just assign this Group Policy to the OU, and you are done. The same task is created on a bunch of computers. The only problem is that all those tasks start at the same time. So, what if you need them to start at slightly different times—let’s say within five minutes or an hour of each other? In the case of Group Policy, you would need to create a bunch of OUs or separate your computers by groups, which from my perspective is not very efficient.

schtasks command

The second way is to use the schtasks command line utility. It allows you to query, create, run, change, and remove tasks on a local or remote computer. If you want to know more about it, you can just check Microsoft's documentation page.

I prefer to use this utility in combination with PowerShell. Let’s take a look at a simple script that creates the same task on two computers with different start times:

$TaskName = "Testtask"
$TaskRun = "C:\Windows\System32\cmd.exe"
$SC = "WEEKLY"
$Days = "MON"
Import-Csv -Path "c:\temp\testtask.csv" | % {
   $system = $_.Host
   $ST = $_.Time
   $Exp = "schtasks /Create /S " + $system + " /SC " + $SC + " /D " + $Days + " /ST " + $ST + " /TN " + $TaskName + " /TR " + $TaskRun
   Invoke-Expression $Exp
}

As you can see, I first assign values to the variables. Then I import the .csv file with the computer names and desired times to run the tasks. I use the foreach-object cmdlet to loop trough them, assigning the computer name and the time to run for each task to the $system and $ST variables. Then I form the expression to run and save it to the $Exp variable. Finally, I run that expression using the Invoke-Expression cmdlet, which runs a specified string as a command. The screenshot from the testtask.csv file is shown below:

Test task csv file

Test task csv file

PowerShell

One more way to manage scheduled tasks that I’d like to mention is to use the Scheduled Tasks cmdlets that Microsoft provides with Windows 10. Unfortunately, I still can’t make much use of them, since most of the environments I working with primary use Windows Server 2008 R2, where those cmdlets are not available. But I can provide a quick demonstration of how they work.

Let’s try to rewrite the above script to use the PowerShell cmdlets:

$TaskName = "Testtask"
Import-Csv -Path "c:\temp\testtask.csv" | % {
   $Time = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At $_.Time
   $TaskRun = New-ScheduledTaskAction -Execute "C:\Windows\System32\cmd.exe"
   $sess = New-CimSession -ComputerName $_.Host
   Register-ScheduledTask -TaskName $TaskName -Trigger $Time -CimSession $sess –Action 
   $TaskRun 
} 

As you already noticed, the script changed a lot.

In fact, the only things that are still the same are the $TaskName variable, the Import-Csv cmdlet, and the foreach-Object loop. To create a trigger for a task, I need to use a special cmdlet: New-ScheduledTaskTrigger.

The same as with a task action, New-ScheduledTaskAction helps to create an action for a scheduled task. That is because the Register-Scheduled task cmdlet, which is used to create a task on local or remote computer, doesn’t accept strings for the –Trigger and –Action arguments but requires them to be objects of the right types.

The cmdlets mentioned above help create those objects. The same thing happened with creating a task on a remote machine. I can’t just provide the remote machine name. Instead, I have to create the CimSession object for it first, using the New-CimSession cmdlet and then using this object for the remote scheduled task creation.

Subscribe to 4sysops newsletter!

If you want to know more about new Scheduled Task cmdlets, you can always check this Microsoft page.

avatar
2 Comments
  1. Rick 6 years ago

    The Group Policy method of using Task Scheduler has not worked since May, 2014, when MS14-025 was implemented via a security update.

    We have never found any way to schedule a task in Computer Configuration -> Preferences -> Control Panel Settings -> Scheduled Tasks that will properly create and execute.  No domain credentials will work and local credentials cannot be used in the GP definition.  NT AUTHORITY\SYSTEM or BUILTIN\SYSTEM also do not work as the defined user.

    • Dan 6 years ago

      I wanted to throw out there that while the MS14-025 update does prevent you from specifying a domain account to run the task, scheduled tasks through policy do work as advertised if you use NT Authority/SYSTEM.  I depend on several of these to manage various tasks in our firm.  Perhaps something environmental is preventing it from working in some scenarios?

Leave a reply

Please enclose code in pre tags

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account