Getting Things Done – Outlook Task Automation with PowerShell

Scott just finished writing about his boss (and now him) using Blat to help him Get Things Done.

I've been "Getting Things Done" for some time, and one thing that's always annoyed me was how difficult it is to convert a desire into a categorized task. Outlook 2007 had the opportunity to make this better with the quick-entry field in the TODO bar, but tasks entered that way unfortunately have a due date for "Tomorrow." While not terrible, it means additional fiddling around – which I wanted to avoid in the first place.

The blat solution is helpful, but still requires an intermediary. Once the reminder is in your inbox, it requires an additional step of triaging to convert it into a task.

Some time back, I wrote the following script to make it easy to directly enter tasks into Outlook from PowerShell. It even validates the category (if you specify one,) so you don't get items in the wrong category:

## Add-OutlookTask.ps1
## Add a task to the Outlook Tasks list

param( $description = $(throw "Please specify a description"), $category, [switch$force )

## Create our Outlook and housekeeping variables. 
## Note: If you don't have the Outlook wrappers, you'll need
## the commented-out constants instead

$olTaskItem = "olTaskItem"
$olFolderTasks = "olFolderTasks"

#$olTaskItem = 3
#$olFolderTasks = 13

$outlook = New-Object -Com Outlook.Application
$task = $outlook.Application.CreateItem($olTaskItem)
$hasError = $false

## Assign the subject
$task.Subject = $description

## If they specify a category, then assign it as well.
if($category)
{
    if(-not $force)
    {
        ## Check that it matches one of their already-existing categories, but only
        ## if they haven't specified the -Force parameter.
        $mapi = $outlook.GetNamespace("MAPI")
        $items = $mapi.GetDefaultFolder($olFolderTasks).Items
        $uniqueCategories = $items | Sort -Unique Categories | % { $_.Categories }
        if($uniqueCategories -notcontains $category)
        {
            $OFS = ", "
            $errorMessage = "Category $category does not exist. Valid categories are: $uniqueCategories. " +
                            "Specify the -Force parameter to override this message in the future."
            Write-Error $errorMessage
            $hasError = $true
        }
    }

    $task.Categories = $category 
}

## Save the item if this didn't cause an error, and clean up.
if(-not $hasError) { $task.Save() }
$outlook = $null

 

One valid point that Scott brought up for using a batch file instead of a PowerShell script is that it was easier to run batch files from places like Start | Run, SlickRun, and apps like that. One thing I sometimes do is write a wrapper batch file:

REM todo.bat
powershell -command "Add-OutlookTask '%1'
'@TODO' -Force"

Another option is to associate PowerShell as the interpreter for .PS1 files. Which reminds me...

[C:\Temp]
PS:44 > Add-OutlookTask
"Write about associating PowerShell with PS1" "@Write"

 

7 Responses to “Getting Things Done – Outlook Task Automation with PowerShell”

  1. Greg Wojan writes:

    Lee,

    Nice! I was just trying to figure out how to work with Outlook 2003. 🙂

    When I run the script it doesn’t find my defined categories — I have to use -force every time. When I step through it this line:

    $items = $mapi.GetDefaultFolder($olFolderTasks).Items

    $items is empty. Any ideas?

    –Greg

  2. Lee writes:

    Greg;

    I would be that this is caused by not having the Outlook programmability wrappers installed. Try changing the constants at the top of the script to the commented out ones.

    Lee

  3. Greg Wojan writes:

    I did change the constants at the top. 🙁

    I’ll try installing the wrappers and see what happens. Thanks.

    –Greg

  4. Outlook Tasks in PowerShell and Vim | Julian Kay's Blog writes:

    […] cool huh? If you’re looking to add Outlook Tasks in a similar way, you can check out this script by Lee Holmes. I use this one with the alias of nt to save on all that extra […]

  5. José Pires writes:

    Hi: my email was wrongly typed in my previous message; sorry. I’m doing it again.

    I’ve installed Add-Outlooktask.ps1 and Autohotkei.ahk; everything run as expected but the task doesn’t appear at outlook task folder… nor elsewhere.

    I have the Outlook 2010 and windows 7.

    May you give me some help?
    Thanks.

    José

  6. Sam Park writes:

    I am trying to get this to work with Outlook 2013. It seems to run, but like Jose Pires, above, it does not seem to add the task. Do you have an updated script?

  7. Sam Park writes:

    You can ignore my last comment. This script still works. However, unless you set Outlook to show tasks with no date, it will not show up on your list.

Leave a Reply