Create Scheduled task to run action in future (Now+XX time)

Ok, so I couldn’t think of a great title, but basically this will allow you to run any action in the future, 15 minutes, 30 minutes, 5 days, whatever you want.

Why?  Because you can.  I’ve used this to have actions run post task sequence, either to assist in a rollback, or trigger additional tasks.

PowerShell code is pretty simple: (Example Create scheduled task to reboot computer in 30 minutes)

$time = [DateTime]::Now.AddMinutes(30)
$hourMinute=$time.ToString("HH:mm")
SchTasks.exe /Create /RU "SYSTEM"/SC ONCE /TN “MyTask” /TR "shutdown.exe /r /t 120" /ST $hourMinute /F

Additional Examples:
Run a Scheduled Task in 2 hours:

$time = [DateTime]::Now.AddHours(2)
$hourMinute=$time.ToString("HH:mm")
SchTasks.exe /Create /RU "SYSTEM"/SC ONCE /TN “RunMyTask” /TR "SchTasks.exe /Run /TN MyTask" /ST $hourMinute /F

Remove Machine from Provisioning Mode in 4 hours:

$time = [DateTime]::Now.AddHours(4)
$hourMinute=$time.ToString("HH:mm")
SchTasks.exe /Create /RU "SYSTEM"/SC ONCE /TN “ProvModeExit” /TR 'Powershell.exe -WindowStyle Hidden -Command Invoke-WmiMethod -Namespace root\CCM -Class SMS_Client -Name SetClientProvisioningMode -ArgumentList $false' /ST $hourMinute /F

I’ve used in this IPU to make sure the computer comes back out of provisioning mode even if it hangs or rolls back. Create this Scheduled task early in the process, but give it like 2 or 3 hours.

This method is pretty limited, and then it doesn’t clean up after itself, so typically what I’ll do, is create a scheduled task that does exactly what, I typically using the gui itself to get close, then edit the XML, and the last action of the scheduled task is to create a run once key that will delete the scheduled task itself on the next reboot.  In my Example below, I’m using a scheduled task to create an autologon, but you can do pretty much anything you can imagine. 

image
image

When I run the Powershell code, it is creating my scheduled task that will run in exactly 30 minutes:

As you can see, scheduled tasks are quite powerful, and having the ability to create them to run in XX amount of time can be handy.

Hope you find this helpful.
Posted on GARYTOWN.COM

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.