Create Microsoft ToDo Task with PowerShell and Microsoft GRAPH API

Now it is time to create some Microsoft ToDo Tasks with PowerShell and MS Graph API.

To learn more from Microsoft GRAPH API, see my Blog Series:
Part 1 – Authentication and Azure App – Use Microsoft Graph API with PowerShell – Part 1 » TechGuy
Part 2 – Oauth2.0 – Use Microsoft Graph API with PowerShell – Part 2 » TechGuy
Part 3 – First Powershell Script to get a Teams List and Walkthrough – Use Microsoft Graph API with PowerShell – Part 3 » TechGuy
Part 4 – Some Examples – Use Microsoft Graph API with PowerShell – Part 4 » TechGuy

API Reference and Permissions

Here is the official Doc: Create todoTask – Microsoft Graph v1.0 | Microsoft Docs

Depending on that, we need the Following Permissions for our Azure App

  • Delagte: Tasks.ReadWrite

The Script


$clientID = "your APP ID"
$User = "your User"
$PW = "your Password"
$resource = "https://graph.microsoft.com"



$MicrosoftToDoListName = "My Tasks"
$title = "A New Task"
$importance = "normal" #Options: high, normal, low
$Body = "Thats my Body Text"

$DueDateTime="2021-08-29T22:00:00.0000000"
$DueTimeTzone="UTC"


#Connect to GRAPH API
$tokenBody = @{  
    Grant_Type = "password"  
    Scope      = "user.read%20openid%20profile%20offline_access"  
    Client_Id  = $clientId  
    username   = $User
    password   = $pw
    resource   = $resource
}   

$tokenResponse = Invoke-RestMethod "https://login.microsoftonline.com/common/oauth2/token" -Method Post -ContentType "application/x-www-form-urlencoded" -Body $tokenBody -ErrorAction STOP
$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}



#Get ID of List
$URLGetToDoLists = "https://graph.microsoft.com/v1.0/me/todo/lists?`$filter=displayName eq '$($MicrosoftToDoListName)'"
$Return = Invoke-RestMethod -Method GET -Headers $headers -Uri $URLGetToDoLists
$ListID = $Return.value.id



#Create a Task
$URLCreateTask = "https://graph.microsoft.com/v1.0/me/todo/lists/$ListID/tasks"
$JsonBody = @"
{
    "title":"$title",
    "importance":"$importance",
    "body":{
        "content":"$Body",
        "contentType":"text"
     },    
     "dueDateTime":{
         "dateTime":"$DueDateTime",
         "timeZone":"$DueTimeTzone"
        },
    "linkedResources":[
       {
          "webUrl":"https://techguy.at",
          "applicationName":"Browser",
          "displayName":"Techguy.at"
       }
    ]
}
"@

$Return = Invoke-RestMethod -Method POST -Headers $headers -Uri $URLCreateTask -Body $JsonBody

The Result

Now you see a Task in Microsoft ToDo create with PowerShell and MS GRAPH API

GitHub Repo

Here you can find lots of other MS GRAPH API examples on my GitHub Repo: Seidlm/Microsoft-Graph-API-Examples (github.com)

Michael Seidl aka Techguy
au2mate everything

Leave a Comment

Your email address will not be published. Required fields are marked *

*

%d bloggers like this: