PoshCode Logo PowerShell Code Repository

New-Task (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/829"></script>download | new post

Allows for the creation of tasks in Microsoft Outlook from Windows PowerShell. The majority of task options available can be configured with the function.

  1. <#
  2. .SYNOPSIS
  3. Creates a task in Outlook
  4. .DESCRIPTION
  5. Allows for the creation of tasks in Microsoft Outlook from Windows PowerShell. The majority of task options available can be configured with the function.
  6. .LINK
  7. http://www.leeholmes.com/blog/GettingThingsDoneOutlookTaskAutomationWithPowerShell.aspx
  8. .EXAMPLE
  9.         C:\PS>New-Task -Subject "Create PowerShell script to create Outlook tasks"
  10.        
  11.         Description
  12.         -----------
  13.         This example just a simple Outlook Task with the specified subject.
  14.  .EXAMPLE
  15.   C:\PS>New-Task -Subject "Create powershell script to create outlook calendar entries" -Categories "%PowerShell" -DueDate "2/01/2009" -Status "InProgress" -Display
  16.  
  17.   Description
  18.   -----------
  19.   This example demonstrates the creation of task using additional options. The '-display' parameter brings up the Outlook task form at the end to allow you make additional changes in the UI.
  20. .INPUTTYPE
  21. .RETURNVALUE
  22. .COMPONENT
  23. .ROLE
  24. .FUNCTIONALITY
  25. .PARAMETER Subject
  26.   Sets the subject of the task.
  27. .PARAMETER Body
  28.   Sets the body of the task.
  29. .PARAMETER Categories
  30.   Sets the categories for the task. Comma seperate the values for multiple categories.
  31. .PARAMETER StartDate
  32.   Sets the start date of the task. Must be in a format that can be converted to [datetime] object.
  33. .PARAMETER DueDate
  34.   Sets the due date of the task. Must be in a format that can be converted to [datetime] object.
  35. .PARAMETER PercentComplete
  36.   Sets the percent complete for the new task. Value must be an int from 0 to 100.
  37. .PARAMETER ReminderTime
  38.   Sets the reminder time of the task. Must be in a format that can be converted to [datetime] object.
  39. .PARAMETER Status
  40.   Sets the the status of the new task. Valid values are "Completed", "Deferred", "InProgress","Waiting"
  41. .PARAMETER Priority
  42.   Sets the priority value of the new task. Valid values are "Low", "Medium", "High"
  43. .PARAMETER Display
  44.         Displays the Outlook task form after saving.
  45. .NOTES
  46. Name: New-Task
  47. Author: Mark E. Schill
  48. Date Created: 01/24/2009
  49. Date Revised: 01/24/2009
  50. Version: 1.0
  51. History: 1.0 01/24/2009 - Initial Revision
  52. #>
  53. [CmdletBinding()]
  54. # TODO: Handle the status and priority
  55. param(
  56. [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
  57. [string]
  58. $Subject
  59. ,
  60. [Parameter(Mandatory=$false, ValueFromPipeline=$false)]
  61. [string]
  62. $Body
  63. ,
  64. # TODO: Be able to specify "today, tomorrow, next week, etc" and convert that to appropriate date.
  65. # http://refactormycode.com/codes/488-parse-relative-date
  66. [Parameter(Mandatory=$false, ValueFromPipeline=$false)]
  67. [DateTime]
  68. $StartDate
  69. ,
  70. # TODO: Be able to specify "today, tomorrow, next week, etc" and convert that to appropriate date.
  71. [Parameter(Mandatory=$false, ValueFromPipeline=$false)]
  72. [DateTime]
  73. $DueDate
  74. ,
  75. [Parameter(Mandatory=$false, ValueFromPipeline=$false)]
  76. [string]
  77. $Status
  78. ,
  79. [Parameter(Mandatory=$false, ValueFromPipeline=$false)]
  80. [string]
  81. $Priority
  82. ,
  83. [Parameter(Mandatory=$false, ValueFromPipeline=$false)]
  84. [int]
  85. $PercentComplete = 0
  86. ,
  87. [Parameter(Mandatory=$false, ValueFromPipeline=$false)]
  88. [DateTime]
  89. $ReminderTime
  90. ,
  91. [Parameter(Mandatory=$false, ValueFromPipeline=$false)]
  92. [string]
  93. $Categories
  94. ,
  95. [Parameter(Mandatory=$false, ValueFromPipeline=$false)]
  96. [switch]
  97. $Display
  98. )
  99. Begin
  100. {
  101.         $olTaskItem = "olTaskItem" <# 3 #>
  102.         $olFolderTasks = "olFolderTasks" <# 13 #>
  103.         $Outlook = New-Object -ComObject Outlook.Application
  104. }
  105. Process
  106. {
  107.         $Task = $Outlook.Application.CreateItem($olTaskItem)
  108.         $ContainsError = $false
  109.  
  110.         $Task.Subject = $Subject
  111.         $Task.PercentComplete = $PercentComplete
  112.         $Task.Body = $Body
  113.         if ( $Categories )  { $Task.Categories = $Categories }  
  114.         if ( $StartDate ) { $Task.StartDate = $StartDate }
  115.   if ( $DueDate ) { $Task.DueDate   = $DueDate }
  116.          
  117.   switch ( $Status )
  118.     {
  119.       "Completed"
  120.         {
  121.           $Task.Status = [Microsoft.Office.Interop.Outlook.OlTaskStatus]::olTaskComplete
  122.           $Task.PercentComplete = 100 # Overrides PercentComplete if specified
  123.         }
  124.        "Deferred" { $Task.Status = [Microsoft.Office.Interop.Outlook.OlTaskStatus]::olTaskDeferred }
  125.        "InProgress" { $Task.Status = [Microsoft.Office.Interop.Outlook.OlTaskStatus]::olTaskInProgress }
  126.        "Waiting" { $Task.Status = [Microsoft.Office.Interop.Outlook.OlTaskStatus]::olTaskWaiting }
  127.        default { $Task.Status = [Microsoft.Office.Interop.Outlook.OlTaskStatus]::TaskNotStarted }
  128.     }
  129.  
  130.   switch ($Priority )
  131.     {
  132.        "Low"  { $Task.Importance = [Microsoft.Office.Interop.Outlook.OlImportance]::olImportanceLow }
  133.        "High" { $Task.Importance = [Microsoft.Office.Interop.Outlook.OlImportance]::olImportanceHigh }
  134.        default { $Task.Importance = [Microsoft.Office.Interop.Outlook.OlImportance]::olImportanceNormal }
  135.     }
  136.  
  137.  
  138.         if ( $ReminderTime )
  139.                 {
  140.                 $Task.ReminderTime = $ReminderTime
  141.                 $Task.ReminderSet = $True
  142.                 }
  143.        
  144.   if ( -not $ContainsError)
  145.     {
  146.                 $Task.Save()
  147.                 if ($Display ) { $Task.Display() }
  148.     }
  149.  
  150. }
  151. End
  152. {
  153.   $Outlook = $Null
  154. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me