Question:
I’m extracting todays appointments with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$olFolderCalendar = 9 $ol = New-Object -ComObject Outlook.Application $ns = $ol.GetNamespace('MAPI') $Start = (Get-Date).AddDays(-1).ToShortDateString() + " 00:00" $End = (Get-Date).AddDays(+1).ToShortDateString() + " 00:00" $Filter = "[MessageClass]='IPM.Appointment' AND [Start] > '$Start' AND [End] < '$End'" $Appointments = $ns.GetDefaultFolder($olFolderCalendar).Items $Appointments.Sort("[Start]") $Appointments.IncludeRecurrences = $false foreach ($Appointment in $Appointments.Restrict($Filter) ) { ... } |
All todays appointments are listed but also a lot of recurring appointments that are NOT taking place today (birthdays, weekly appointments, …). Any idea how to avoid that?
EDIT: Seems like all this unwanted appointments are originally from my mobile synced to outlook. I’ll try the script on a ‘clean’ PC.
EDIT: I tried the script on another PC without synced elements and it’s the same: All recurring elements are display whether they are today or not.
AND [IsRecurring] = ‘$False’
is not helping either.
Answer:
The initial query must include the original appointment of the series, so if the series started 3 months ago, the collection of appointments ($folder.items) date range must be set accordingly.
Afterwards you can filter for the desired date range.
This code works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Function Get-OutlookCalendar { echo starting... Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null $olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type] $outlook = new-object -comobject outlook.application $namespace = $outlook.GetNameSpace("MAPI") $folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar) $a = Get-Date -Hour 0 -Minute 00 -Second 00 $b = (Get-Date -Hour 0 -Minute 00 -Second 00).AddDays(7) echo From $a To $b $Appointments = $folder.Items $Appointments.IncludeRecurrences = $true $Appointments.Sort("[Start]") $Appointments | Where-object { $_.start -gt $a -AND $_.start -lt $b } | Select-Object -Property IsRecurring, RecurrenceState, Subject, Start, Location } #end function Get-OutlookCalendar |
To run this -for dummies, like myself yesterday 🙂
1 2 3 4 5 |
cmd powershell PS C:\Users\Jose\Documents\WindowsPowerShell> Import-Module -Name Outlook\expcal.psm1 -Force PS C:\Users\Jose\Documents\WindowsPowerShell> Get-OutlookCalendar |