PowerShell provides the functionality to access event and meeting details in the calendars of Exchange users. In this article, we aim to explore how to generate a list of calendar events and how to remove a particular event or appointment from all user calendars in an Exchange Server or Microsoft 365 organization.
Procedure to List and Read User Calendar Events in Exchange Online (Microsoft 365)
To begin with, let's understand how to generate a list of calendar events in a user's mailbox on Exchange Online (Microsoft 365). Microsoft Graph API is proficient in retrieving information about the calendar items in the user's mailbox. Begin by registering a new application on Azure by following this path (Microsoft Entra ID -> App registration -> New registration). Following this, accord the Microsoft Graph permissions for: Calendars.Read and Calendars.ReadBasic.All.
Now you can use the Microsoft.Graph module to connect to the Microsoft 365 tenant. In this example, we will use certificate-based Azure authentication in PowerShell.
$certThumbprint = "9CF05589A4B29BECEE6456F08A76EBC3DC2BC581"
$AzureAppID = "111111-2222-3333-4444-12345678"
$tenant="woshub.onmicrosoft.com"
Connect-MgGraph -AppId $AzureAppID -CertificateThumbprint $certThumbprint -TenantId $tenant
Install-Module Microsoft.Graph -Scope AllUsers
To view events in a particular user’s calendar for the current month:
$StartDate = (Get-Date -Day 1)
$EndDate = (Get-Date -Day 1).Addmonths(1)
Get-MgUserCalendarView -UserId [email protected] -CalendarId "Calendar" -StartDateTime $StartDate -EndDateTime $EndDate | Select-Object -Property @{Name='EventStart';Expression={ $_.Start.DateTime}},@{Name='EventEnd';Expression={ $_.End.DateTime}},Subject, BodyPreview
The command returned the event subjects, their contents (body preview), and their start/end times.
Removing Event from Exchange Calendar Using PowerShell
Use the Remove-CalendarEvents cmdlet to cancel (delete) an event (appointment, meeting) in Exchange calendars. It works for both Exchange Online and Exchange Server 2019 organizations.
Open the PowerShell console and connect to your Exchange Online tenant (using the Exchange Online PowerShell module) or the Exchange Server host (remote connection to Exchange server without EMS installation).
Removal of all upcoming events (within the next 15 days) from a user’s calendar for which they are the organizer:
Remove-CalendarEvents -Identity [email protected] -CancelOrganizedMeetings -QueryWindowInDays 15
Alternatively, you can delete all events starting from a specific date:
Remove-CalendarEvents -Identity [email protected] -CancelOrganizedMeetings -QueryStartDate 04-10-2023 -QueryWindowInDays 60
You can use the -PreviewOnly -Verbose options to view a list of such events without deleting them.
The main disadvantage of the Remove-CalendarEvents cmdlet is that it can only remove upcoming calendar events and does not allow you to select events by subject, organizer, content, or any other property. The most common use of Remove-CalendarEvents is to quickly clear a user’s calendar of events that were created by an employee who has been fired, is off sick, or has gone on holiday.
If you need to delete past events, or if you are using on-premises Exchange Server 2016/2013/2010, you can use the Search-Mailbox or New-ComplianceSearch cmdlets to search and delete items in Exchange mailboxes.
On Exchange Server, for instance, you can locate calendar events with a specific subject in the following way:
Search-Mailbox -Identity [email protected] -SearchQuery {Subject:"Discuss: AD Schema Update" AND Kind:meetings AND Received:01/12/2023..15/01/2024} -TargetMailbox report_mbx -TargetFolder SearchMailboxResult –LogOnly -LogLevel Full
This command lets you search for an event within the user's mailbox, storing the findings in the SearchMailboxResult folder of the report_mbx mailbox.
If you would like to delete the discovered event, substitute the final parameters with -DeleteContent. Here's the method to delete all user's calendar events:
SearchMailbox -identity [email protected] -SearchQuery kind:meetings -DeleteContent
In Exchange Online, the SearchMailbox cmdlet is deprecated and you have to use the ComplianceSearch cmdlets instead. For example, to find all events with a specific subject in all mailboxes, run the commands:
New-ComplianceSearch -Name DeleteITMeeting -ContentMatchQuery "kind:meetings AND subject:weekly_it_meeting" -ExchangeLocation all
Start-ComplianceSearch -identity DeleteITMeeting
Get-ComplianceSearch -identity DeleteITMeeting | fl
Once the task is complete (Status=Completed), you can delete the events it has found:
New-ComplianceSearchAction -SearchName DeleteITMeeting -Purge
