How to create a scheduled task using Intune

In my latest blog post, I have discussed about how to deploy powershell script using Intune which is great to deploy one off scripts on target devices. Another approach to deploy powershell scripts is to use Intune device remediations which we will use in this blog post to create a scheduled task.

A scheduled task in Windows allows you to automate the execution of programs, scripts, or commands at specified times or events. It is part of the Windows Task Scheduler, which is a built-in tool in Windows operating systems.

With scheduled tasks, you can perform various actions, such as running backups, scheduled reboots, performing system maintenance, launching scripts or programs, sending emails, and much more. It’s a powerful tool that can help improve productivity and automate repetitive tasks.

You can easily create a scheduled task manually in Windows using Task scheduler application. Just open the application and click on Action > Create Basic task. However, when you are dealing with 100’s of Windows 10 or Windows 11 devices, you have to find an automated way of deploying a scheduled task with minimum effort from your side.

Today, we will use Intune Device remediations feature for creating a scheduled task on a Windows 10/11 device. Device remediations is also referred to as Intune proactive remediations. We will use Powershell scripts to detect the existence of a schedule task by its name and if it does not exist on the device, a Remediation script will be called to create a schedule task.

We have below two powershell scripts, one for Detection of a given scheduled task and one for Remediation. We will Deploy/Create a scheduled task to Restart a Windows device every day at 3AM using Intune.

Below Detection script will check if a scheduled task named Cloudinfra-RebootDevice exists on the target device or not.

Detection_scheduletask.ps1

<#
.DESCRIPTION
    This script checkes if Cloudinfra-RebootDevice schedule task exists on the device or not
    Author: Jatin Makhija
    Site: cloudinfra.net
    Version: 1.0.0
#>
$taskName = "Cloudinfra-RebootDevice"
$taskStatus = get-scheduledtask | Where-object {$_.taskName -eq "Cloudinfra-RebootDevice"}
if ($taskStatus){
    Write-Host "Task already Exists. No Action Needed."
    Exit 0
}
Else{
    Write-Host "Task does not exist, Remediation required"
    Exit 1
}

Below Remediation script will check to see if a scheduled task named Cloudinfra-RebootDevice exists on the target device or not. If it does not exist then it will create it on the device.

Remediation_scheduletask.ps1

<#
.DESCRIPTION
    This script checkes if Cloudinfra-RebootDevice schedule task exists on the device or not. If 
    it does not exist. It will Create a Task name Cloudinfra-RebootDevice.
    Author: Jatin Makhija
    Site: cloudinfra.net
    Version: 1.0.0
#>
$taskName = "Cloudinfra-RebootDevice"
$taskstatus = get-scheduledtask | Where-object {$_.taskName -eq "Cloudinfra-RebootDevice"}
if (!$taskstatus){
    try{
        Write-Host "Cloudinfra reboot device task does not Exists. Creating Task."
        $STaction  = New-ScheduledTaskAction -Execute 'c:\windows\system32\shutdown.exe' -Argument '-r -t 0'
        $STtrigger = New-ScheduledTaskTrigger -Daily -At 3am
        $STSet     = New-ScheduledTaskSettingsSet
        $STuser    = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
        Register-ScheduledTask -TaskName "Cloudinfra-RebootDevice" -TaskPath "\"  -Action $STaction -Settings $STSet -Trigger $STtrigger -Principal $STuser
        Exit 0
    }
    Catch {
            Write-Host "Error in Creating scheduled task"
            Write-error $_
            Exit 1
    }
}
Else{
        Write-Host "Task already Exists, No Remediation required"
        Exit 1
}

Create Remediation Script Package in Intune

We will use the above Detection and Remediation scripts to create a script package using Intune device Remediations. Let’s check the steps:

Basics Tab

Provide the Name and Description of the package. Keep rest of the settings as default.

  • Name: Restart Device Everyday Scheduled Task
  • Description: This Remediation will create a scheduled task on a Windows device for restarting a windows computer everyday at 3AM
  • Publisher: Jatin Makhija (auto-filled)
  • Version: Auto-filled

Settings Tab

Browse to the Detection script and Remediation Script file. You need to save above scripts in two powershell script files with extension as .ps1 and then browse to it using blue folder Icon as shown in below screenshot.

  • Detection script file – Browse to the Detection script Detection_scheduletask.ps1
  • Remediation script file – Browse to Remediation script file Remediation_scheduletask.ps1
  • Run this script using the logged-on credentials – No
  • Enforce script signature check – No
  • Run script in 64-bit Powershell – Yes
Intune remediation script settings for deployment of a Scheduled Task
Intune remediation script settings for deployment of a Scheduled Task

Assignments tab

Create an Azure AD Security group which contains devices where this custom script package needs to be deployed. Please note that if you add users into the list, a scheduled task will be created on all the devices which are assigned to that user. If you want to deploy it to specific devices then you should add devices in the Azure AD security group not users.

To deploy it on all end user devices, You can click on + Add all devices to target all devices which are enrolled into Intune. You can also choose the schedule of executing the script package. You have three options, Oncehourly and Daily.

I prefer to select Hourly and Repeat every 1 hour when I am testing the deployment on couple of devices. This way the device remediation script package is executed every 1 hour to check whether a schedule task exists on the device or not. I get the results quickly and can update the script package if there are any issues.

However, once you have established that the Remediation scripts are working as expected and testing was successful, you can change it to run less frequently like Daily at a specified time.

Intune remediation script Schedule
Intune remediation script Schedule

Review + Create

On Review + Create tab, review the remediations script package details and click on Create. As soon as you click on create button, device remediation script package deployment will start and the process to create a scheduled task on windows device will begin.

Intune Policy Refresh Cycle

The Device will Sync / Check in to start the Remediation process. It may take some time for the process to start. Therefore, if you are testing it on a test device, you can force initiate Intune refresh cycle on the device which will speed up the scripts download, execution and remediation process. You can also use Powershell to force initiate Intune refresh cycle.

Also, you can restart the device first which also starts the device check-in process. Manual sync is not mandatory on user’s devices as the device check-in process happens automatically. But if you are testing the script package on a test device then this can speed up your testing and can save some time.

End user Experience

Now, let’s check whats happening on end user device. After the Remediation script package has been executed successfully. You can open Task Scheduler App and check if the scheduled task has been created. Please follow below steps to check:

  • Go to Start and search for Task Scheduler application
  • Launch the application and click on Task Scheduler Library
  • You can find a new Scheduled task created called as Cloudinfra-RebootDevice. Its configuration like action, trigger etc. is configured as per the remediation powershell script
Verifying scheduled task deployment on a Windows device
Verifying scheduled task deployment on a Windows device

How to Monitor Intune device Remediations

You can monitor Intune device remediation script packages from Intune admin center by following below steps:

  • Login on Microsoft Intune admin center
  • Go to Devices Remediations
  • Click on the Remediation script package you want to monitor. “Restart Device Everyday Scheduled Task
How to Monitor Intune device Remediations
How to Monitor Intune device Remediations

Go to the Overview page and check the Detection Script and Remediation Script status. As you can see in below screenshot that our Detection script has found issues that means that it was not able to find the schedule task Cloudinfra-RebootDevice on the device . Remediation Status shows as “Issue Fixed” that means remediation powershell script was able to create a schedule task on the target device successfully.

Detection and Remediation Monitoring for Intune Proactive Remediations
Detection and Remediation Monitoring for Intune Proactive Remediations

Where to find Intune Remediation Logs

You can find Intune device remediation logs in IntuneManagementExtension.log file, please follow below steps to locate this log file and find the logs related to this script package deployment

  • Browse to C:\ProgramData\Microsoft\IntuneManagementExtension\Logs and look for most recent version of IntuneManagementExtension.log file
  • Best way to open the file is using CMTrace tool
Where to find Intune Remediation Logs
Where to find Intune Remediation Logs

How to create a scheduled task to run a Powershell script

You could have a requirement to execute powershell script on a schedule. A scheduled task can be created for this the same way we have created a scheduled task to reboot the device in previous steps.

Use the same script and just update the variable $STaction which runs New-ScheduledTaskAction cmdlet and provide powershell script path. Make sure to first copy the powershell script on the target devices before providing its path in the script.

Here’s an example of the updated $STaction variable which executes a powershell script stored at C:\AllScripts\RestartWindows.ps1 path.

$STaction

$STaction = New-ScheduledTaskAction -Execute '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"' -Argument '-ExecutionPolicy Bypass -File "C:\AllScripts\RestartWindows.ps1"'

Conclusion

In this blog post, we have seen how to create a scheduled task on a Windows 10 or Windows 11 device using Intune. There are different methods for creating a scheduled task using Intune. However, I found this one works best and you can customize is easily based on your requirement.

You can edit remediation script and create a scheduled task as per your requirement. In this blog post, I took an example of creating a scheduled task to restart windows devices every day at 3 AM.

READ NEXT