Skip to content

Instantly share code, notes, and snippets.

@mattiaswolff
Created September 21, 2012 09:46
Show Gist options
  • Save mattiaswolff/3760657 to your computer and use it in GitHub Desktop.
Save mattiaswolff/3760657 to your computer and use it in GitHub Desktop.
Powershell: Summarize outlook appointments by category
# Powershell script to retrieve all appointments for a given date range using the Outlook Object Model
param ( [DateTime] $rangeStart = [DateTime]::Now
, [int] $weeks = 4)
$outlook = New-Object -ComObject Outlook.Application
# Ensure we are logged into a session
$session = $outlook.Session
$session.Logon()
$olFolderCalendar = 9
$apptItems = $session.GetDefaultFolder($olFolderCalendar).Items
$apptItems.Sort("[Start]")
$apptItems.IncludeRecurrences = $true
$rangeEnd = [DateTime]::Now.AddDays(7 * $weeks);
while (!($rangeEnd.dayOfWeek -eq "Sunday")) { $rangeEnd = $rangeEnd.AddDays(1) };
write-host "Start date: " $rangeStart
write-host "End date: " $rangeEnd
write-host " "
$restriction = "[End] >= '{0}' AND [Start] <= '{1}'" -f $rangeStart.ToString("g"), $rangeEnd.ToString("g")
$categories = @()
$categoriesSummary = @{}
foreach($appt in $apptItems.Restrict($restriction))
{
$di = [Globalization.DateTimeFormatInfo]::CurrentInfo
$apptWeek = $di.Calendar.GetWeekOfYear($appt.start, $di.CalendarWeekRule, $di.FirstDayOfWeek)
if (!($categoriesSummary.keys -contains $apptWeek)) {
$categoriesSummary.add($apptWeek , @{})
}
if (!($categoriesSummary[$apptWeek].keys -contains $appt.categories)) {
$categoriesSummary[$apptWeek].add($appt.categories, 0)
}
$categoriesSummary[$apptWeek][$appt.categories] = $categoriesSummary[$apptWeek][$appt.categories] + ([int]$appt.duration / 60)
}
foreach ($week in ($categoriesSummary.keys | sort-object)) {
$weekHours = ($categoriesSummary[$week].values | measure-object -sum).sum
##write-host "Week:" $week "( total hours:" ($categoriesSummary[$week].values | measure-object -sum).sum")"
$categoriesSummaryWeek = @()
foreach ($category in $categoriesSummary[$week].keys) {
$categorySummaryWeek = @{}
$categorySummaryWeek.category = $category
$categorySummaryWeek.hours = ($categoriesSummary[$week][$category] | measure-object -sum).sum
$categorySummaryWeek.percentage = "{0:P0}" -f (($categoriesSummary[$week][$category] | measure-object -sum).sum / $weekHours)
$categoriesSummaryWeek += new-object psobject -property $categorySummaryWeek | select-object category,hours,percentage
}
$categoriesSummaryWeek
write-host ""
}
@RobSheppard1976
Copy link

Just curious, does this need to be tweaked to work with newer versions of Outlook? I took a quick look at it, as I was looking for something exactly like this, but didn't have a chance to troubleshoot when it it threw an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment