Forum Discussion
How to delete all calendar appointments booked by terminated users?
You can use PowerShell commands to delete all calendar appointments booked by terminated users in your Office 365 environment. Here are the steps to do this:
Connect to Office 365 PowerShell using the following command:
Connect-ExchangeOnline -UserPrincipalName <UPN of a global administrator>
Next, use the following command to get all terminated users:
$terminatedUsers = Get-User -Filter {UserAccountControl -eq "AccountDisabled" -and RecipientTypeDetails -eq "UserMailbox"}
For each terminated user, you need to give full access permission to their mailbox using the following command:
Add-MailboxPermission -Identity <Terminated User's email address> -User <Your Email Address> -AccessRights FullAccess -InheritanceType All
Once you have full access to the terminated user's mailbox, you can use the following command to delete all calendar appointments booked by the user:
Search-Mailbox -Identity <Terminated User's email address> -SearchQuery 'kind:calendar' -DeleteContent -Force
Finally, you can send an email to all attendees to inform them about the deleted meeting using the following command:
Send-MailMessage -To <Attendee email addresses> -Subject "Meeting Cancellation" -Body "The meeting booked by a terminated user has been cancelled." -SmtpServer <SMTP server name>
Note: Before performing any of these steps, please make sure you have appropriate permissions and have tested the commands in a non-production environment. Also, ensure that you have a backup of all the data before you delete anything.
I just need to do this for one user