GitHub Icon Image
GitHub

Cleanup completed Microsoft To Do tasks

Summary

Microsoft To Do is my task management tool of choice, I use it for tracking everything I do, which means I generate and complete a lot of tasks during a working week. This script iterates across all of the task lists and removes the tasks that have been marked as complete.

Warning

Please be aware this script contains a command that will remove or delete an artifact, ensure you test and understand the implications of running the script.

  • CLI for Microsoft 365 with PowerShell
  • CLI for Microsoft 365 with Bash
Write-Output "Getting Microsoft To Do task lists ..."
$lists = m365 todo list list -o json | ConvertFrom-Json

Write-Output "Iterating Microsoft To Do task lists ..."
$lists | ForEach-Object { 
    $listId = $_.Id
    
    Write-Output "Getting completed tasks from '$($_.displayName)' task list ..."
    $tasks = m365 todo task list --listId $listId -o json --query '[?status==`completed`]' | ConvertFrom-Json
    Write-Output "$($tasks.Count) completed tasks found ..."

    $tasks | ForEach-Object {
        Write-Output "Removing '$($_.Title)' task ..."
        m365 todo task remove --listId $listId --id $_.Id --confirm
    }
}

Write-Output "Done"

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

#!/usr/bin/env bash
# -*- coding: utf-8 -*- 

echo "Getting Microsoft To Do task lists ..."
strListIds=`m365 todo list list --query '[].id'`
arrListIds=($strListIds)

echo "Iterating Microsoft To Do task lists ..."
for strlistId in "${arrListIds[@]}"; do
    echo "Getting completed tasks from '${strlistId}' task list ..."
    strTaskIds=$(m365 todo task list --listId "${strlistId}" --query '[?status==`completed`].id')
    arrTaskIds=($strTaskIds)
    strCount=${#arrTaskIds[@]}
    echo "${strCount} completed tasks found ..."    
    for strTaskId in "${arrTaskIds[@]}"; do
        echo "Removing '${strTaskId}' task ..."
        m365 todo task remove --listId "${strlistId}" --id "$strTaskId" --confirm
    done
done

echo "Done"

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Source Credit

Sample first appeared on Cleanup completed Microsoft To Do tasks | CLI for Microsoft 365

Contributors

Author(s)
Garry Trinder

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI