“No Calendar Left Behind” – Automating Exchange Online Access with this Powershell Script

Hey you!

Are you tired of manually granting access to calendars and agendas in Exchange Online for each user in your organization? Do you wish there was an easier and more efficient way to get this done? Well, have no fear, because I’ve got just the solution for you!

Introducing the Powershell script that automates calendar access in Exchange Online and could potentially leave your Service Desk team unemployed! (Just kidding, I don’t want to start any HR issues)

I am sorry… Don’t fire me

The Problem:

Let’s face it, manually granting access to calendars and agendas for each user in your organization is tedious and time-consuming. You might forget to apply it to some users, or worse, apply it to the wrong user. And to make matters worse, not every user has the same folder. Some have Agenda, some have Calendar, and some even have Kalendar (we don’t know what language that is either).

01001110 01101111 01110011 01111001 00100000 01100001 01110010 01100101 00100000 01111001 01101111 01110101

The Solution:

Enter my trusty Powershell script! This script connects to Exchange Online and adds a specific permission to calendars and agendas of all user mailboxes in the tenant. And the best part? It’s fully customizable to exclude certain users from this process. So, if your Management Team says that some users will not be sharing their calendar, you can simply add them to a list and the script will skip them.

The Features:

  • This script is not just efficient, it’s also loaded with features. Here are some of the key features:
  • Automatically connects to Exchange Online and prompts for credentials
  • Retrieves a list of all user mailboxes and a list of users that should not be imported in this list from a text file
  • Checks if the mailbox has a specific folder (calendar, agenda, or kalender) and adds a specific permission (Reviewer) to that folder for a specific user (Read_Group)
  • Provides a progress report for each mailbox to keep you informed of the script’s progress
# Import the Exchange Online PowerShell module
Import-Module ExchangeOnlineManagement

# Connect to Exchange Online
$UserCredential = Get-Credential
Connect-ExchangeOnline -Credential $UserCredential -ShowProgress $true

# Get a list of all user mailboxes by alias
$Mailboxes = Get-Mailbox -ResultSize Unlimited

#Get a list of users that should NOT be imported in this list
$NoSharePLZ = Get-Content -Path C:\Script\NoSharePLZ.txt

ForEach ($Mailbox in $Mailboxes){

    $GetAlias = $($Mailbox.Alias)

    if ($NoSharePLZ -imatch $GetAlias){
    
        Write-Host "This is mailbox $Mailbox and will be ignored"

    } else {

        Write-Host "Checking for $Mailbox"

        # Check if the mailbox has a "calendar" folder
        $hasCalendarFolder = (Get-MailboxFolderStatistics -Identity $Mailbox.Name | Where-Object {$_.FolderPath -eq '/Calendar'}) -ne $null

        # Check if the mailbox has an "agenda" folder
        $hasAgendaFolder = (Get-MailboxFolderStatistics -Identity $Mailbox.Name | Where-Object {$_.FolderPath -eq '/Agenda'}) -ne $null

        # Check if the mailbox has a "kalender" folder
        $hasKalenderFolder = (Get-MailboxFolderStatistics -Identity $Mailbox.Name | Where-Object {$_.FolderPath -eq '/Kalender'}) -ne $null

        # Add permission to the "calendar" folder if it exists
        if ($hasCalendarFolder) {
            Write-Host "$Mailbox has calendar"
            $existingPermission = Get-MailboxFolderPermission -Identity "$($Mailbox.Name):\calendar" -User Read_Group -ErrorAction SilentlyContinue
            if (!$existingPermission) {
                add-MailboxFolderPermission -Identity "$($Mailbox.Name):\calendar" -User R_STND_Agenda_Lezen -AccessRights Reviewer -ErrorAction SilentlyContinue
                Write-Host "$Mailbox calendar done"
            }
            else {
                Write-Host "$Mailbox calendar already has the required permission"
            }
        }

        # Add permission to the "agenda" folder if it exists
        if ($hasAgendaFolder) {
            Write-Host "$Mailbox has agenda"
            $existingPermission = Get-MailboxFolderPermission -Identity "$($Mailbox.Name):\agenda" -User R_STND_Agenda_Lezen -ErrorAction SilentlyContinue
            if (!$existingPermission) {
                add-MailboxFolderPermission -Identity "$($Mailbox.Name):\agenda" -User Read_Group -AccessRights Reviewer -ErrorAction SilentlyContinue
                Write-Host "$Mailbox agenda done"
            }
            else {
                Write-Host "$Mailbox agenda already has the required permission"
            }
        }

        # Add permission to the "kalender" folder if it exists
        if ($hasKalenderFolder) {
            Write-Host "$Mailbox has Kalendar"
            $existingPermission = Get-MailboxFolderPermission -Identity "$($Mailbox.Name):\kalender" -User Read_Group -ErrorAction SilentlyContinue
            if (!$existingPermission) {
                add-MailboxFolderPermission -Identity "$($Mailbox.Name):\kalender" -User R_STND_Agenda_Lezen -AccessRights Reviewer -ErrorAction SilentlyContinue
                Write-Host "$Mailbox agenda done"
            }
            else {
                Write-Host "$Mailbox agenda already has the required permission"
            }
        }
                
    }

}

Conclusion:

In conclusion, this Powershell script is the perfect solution for automating calendar access in Exchange Online. It saves time, reduces errors, and can potentially leave your Service Desk team unemployed (just kidding, I am not that mean). So, give it a try and let me know what you think!