Powershell: Set New Task Under Task Scheduler from Intune – A Step-by-Step Guide
Image by Deen - hkhazo.biz.id

Powershell: Set New Task Under Task Scheduler from Intune – A Step-by-Step Guide

Posted on

Are you tired of manually configuring tasks on multiple devices? Do you want to automate repetitive tasks and save time? Look no further! In this article, we’ll show you how to use PowerShell to set a new task under Task Scheduler from Intune. Yes, you read that right – we’ll take you through a step-by-step process to create and schedule tasks remotely using PowerShell and Intune.

Why Use PowerShell and Intune?

Powershell is an incredibly powerful tool that allows you to automate a wide range of tasks, from simple to complex. When combined with Intune, you can remotely manage and configure devices across your organization. By setting up tasks under Task Scheduler, you can ensure that tasks are executed at specific times or intervals, without requiring manual intervention.

Benefits of Using PowerShell and Intune

  • Automate repetitive tasks: PowerShell and Intune allow you to automate tasks, freeing up time for more critical tasks.
  • Centralized management: With Intune, you can manage devices remotely, making it easy to configure and schedule tasks.
  • increased efficiency: By automating tasks, you can reduce the risk of human error and increase overall efficiency.
  • Customization: PowerShell provides flexibility to customize tasks to meet specific business needs.

Prerequisites

Before we dive into the process, make sure you have the following:

  • Intune account with administrative privileges
  • Powershell installed on your device (version 3 or later)
  • Device(s) enrolled in Intune
  • Familiarity with PowerShell and Intune basics

Step 1: Connect to Intune Using PowerShell

Open PowerShell on your device and run the following command to connect to Intune:

Connect-MSGraph -TenantId <your_tenant_id>

Replace `` with your actual tenant ID. Once connected, you’ll see a notification indicating that you’re now connected to Microsoft Graph.

Step 2: Create a New Task Under Task Scheduler

Next, we’ll create a new task under Task Scheduler using PowerShell. Run the following command:

$task = New-ScheduledTask -Action (New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-Command " YOUR_COMMAND_HERE "' -WorkingDirectory 'C:\') -Description 'My New Task' -TaskName 'MyNewTask' -TaskPath '\' -RunLevel Highest

Replace `YOUR_COMMAND_HERE` with the actual command you want to execute. For example, if you want to run a script, replace it with the script path.

Understanding the Command

Let’s break down the command:

  • `New-ScheduledTask`: Creates a new task under Task Scheduler.
  • `-Action`: Specifies the action to perform when the task is executed.
  • `New-ScheduledTaskAction`: Creates a new action for the task.
  • `-Execute ‘powershell.exe’`: Specifies the executable to run (in this case, PowerShell).
  • `-Argument ‘-Command ” YOUR_COMMAND_HERE “‘`: Specifies the argument to pass to the executable (in this case, the command to execute).
  • `-WorkingDirectory ‘C:\’`: Specifies the working directory for the task.
  • `-Description ‘My New Task’`: Sets the description for the task.
  • `-TaskName ‘MyNewTask’`: Sets the name of the task.
  • `-TaskPath ‘\’`: Specifies the task path (in this case, the root directory).
  • `-RunLevel Highest`: Sets the run level to highest (optional).

Step 3: Set Task Schedule

Next, we’ll set the schedule for the task. You can choose from various triggers, such as daily, weekly, or monthly. For this example, we’ll set a daily trigger:

$trigger = New-ScheduledTaskTrigger -Daily -At 08:00 AM

This command sets a daily trigger at 8:00 AM.

Understanding the Trigger

Let’s break down the trigger:

  • `New-ScheduledTaskTrigger`: Creates a new trigger for the task.
  • `-Daily`: Specifies the type of trigger (in this case, daily).
  • `-At 08:00 AM`: Specifies the time of day to execute the task.

Step 4: Register the Task

Finally, we’ll register the task with Task Scheduler:

Register-ScheduledTask -TaskName 'MyNewTask' -TaskPath '\' -Force

This command registers the task with Task Scheduler.

Step 5: Deploy the Task to Intune

Now that we’ve created and registered the task, we’ll deploy it to Intune:

New-IntuneDeviceConfigurationPolicy -DisplayName "My New Task" -Description "Automated task using PowerShell" -Payload @($task, $trigger)

This command creates a new device configuration policy in Intune and assigns the task and trigger to it.

Step 6: Assign the Policy to Devices

Finally, we’ll assign the policy to devices enrolled in Intune:

New-IntuneDeviceConfigurationPolicyAssignment -PolicyId  -GroupId 

Replace `` with the ID of the policy created in Step 5, and `` with the ID of the group containing the devices you want to target.

Conclusion

That’s it! You’ve successfully created and deployed a new task under Task Scheduler using PowerShell and Intune. With this step-by-step guide, you can automate repetitive tasks and increase efficiency across your organization. Remember to customize the script to meet your specific business needs and don’t hesitate to explore more advanced features of PowerShell and Intune.

Command Description
Connect-MSGraph Connects to Microsoft Graph
New-ScheduledTask Creates a new task under Task Scheduler
New-ScheduledTaskAction Creates a new action for the task
New-ScheduledTaskTrigger Creates a new trigger for the task
Register-ScheduledTask Registers the task with Task Scheduler
New-IntuneDeviceConfigurationPolicy Creates a new device configuration policy in Intune
New-IntuneDeviceConfigurationPolicyAssignment Assigns the policy to devices enrolled in Intune

Remember to replace placeholders with actual values and adjust the script according to your specific requirements. Happy automating!

Frequently Asked Question

Get ready to unlock the power of PowerShell and Intune! Here are some frequently asked questions about setting new tasks under Task Scheduler from Intune, answered just for you!

Q1: What is the PowerShell script to create a new task in Task Scheduler from Intune?

You can use the following PowerShell script to create a new task in Task Scheduler from Intune:

`$taskName = “MyNewTask”
$script = “C:\Path\To\Script.ps1”
$trigger = New-ScheduledTaskTrigger -AtStartup
$action = New-ScheduledTaskAction -Execute “PowerShell.exe” -ArgumentList (“-ExecutionPolicy”, “Bypass”, “-File”, $script)
Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $action`

Just replace `$script` with the path to your PowerShell script, and `$taskName` with the desired name for your task!

Q2: How do I specify the execution policy for the PowerShell script in Task Scheduler?

To specify the execution policy for the PowerShell script in Task Scheduler, you can use the `-ExecutionPolicy` parameter when creating the task action. For example:

`$action = New-ScheduledTaskAction -Execute “PowerShell.exe” -ArgumentList (“-ExecutionPolicy”, “Bypass”, “-File”, $script)`

This will set the execution policy to “Bypass”, allowing the script to run without restrictions. You can adjust this to fit your organization’s security policies!

Q3: Can I set the task to run with elevated privileges in Task Scheduler?

Yes, you can! To set the task to run with elevated privileges in Task Scheduler, you can use the `-RunLevel` parameter when creating the task. For example:

`$principal = New-ScheduledTaskPrincipal -UserId “NT AUTHORITY\SYSTEM” -RunLevel Highest
Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $action -Principal $principal`

This will run the task with the highest privileges, allowing it to perform actions that require administrative access. Be cautious when using this, as it can be a security risk if not properly configured!

Q4: How do I deploy the PowerShell script to Intune-managed devices?

To deploy the PowerShell script to Intune-managed devices, you can create a PowerShell script package in Intune and assign it to a device group. Here’s a high-level overview:

1. Create a new PowerShell script package in Intune.
2. Upload your PowerShell script to the package.
3. Configure the package settings, such as the execution policy and script arguments.
4. Assign the package to a device group in Intune.

Once deployed, the script will run on the targeted devices according to the schedule you specified in Task Scheduler. Easy peasy!

Q5: What happens if I need to update the PowerShell script or task settings?

If you need to update the PowerShell script or task settings, you can simply update the Intune script package and redeploy it to the targeted devices. This will overwrite the existing script and task settings with the new ones.

For task settings, you can use the `Set-ScheduledTask` cmdlet to update the task properties, such as the trigger or action. For example:

`Set-ScheduledTask -TaskName $taskName -Trigger $newTrigger`

Make sure to test your updates thoroughly to avoid any issues on the targeted devices!