← Back to articles PowerShell

Automate Microsoft Teams Calendar Management with PowerShell: A Step-by-Step Guide

Automate Microsoft Teams Calendar Management with PowerShell: A Step-by-Step Guide

Introduction

As an IT admin, managing calendars in Microsoft Teams and Exchange can be a cumbersome task, especially when dealing with room mailboxes and calendar permissions. In this tutorial, you will learn how to automate the management of Teams calendars in Outlook using PowerShell. This guide provides a step-by-step approach to grant send-on-behalf permissions for room mailboxes and manage calendar access efficiently, saving you valuable time.

Prerequisites

  • PowerShell: Latest version installed with the necessary permissions to connect to Exchange Online.
  • Exchange Online Management Module: Make sure this module is installed. You can install it using Install-Module -Name ExchangeOnlineManagement.
  • Access Credentials: Ensure you have the appropriate credentials to access your Exchange Online account.
  • Room Mailboxes: Familiarity with room mailbox capabilities within your organization.

Step 1: Force TLS 1.2

# Force TLS 1.2 (Fixes 'Connection Closed' errors)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Running this code will ensure your PowerShell session uses TLS 1.2, which is necessary for secure connections with Exchange Online.

Step 2: Connect to Exchange Online

Import-Module ExchangeOnlineManagement -ErrorAction Stop

Write-Host "Cleaning sessions and connecting..." -ForegroundColor Cyan

Disconnect-ExchangeOnline -Confirm:$false -ErrorAction SilentlyContinue

try {
    # Using DisableWAM to avoid the window handle bug
    Connect-ExchangeOnline -DisableWAM -ShowBanner:$false
}
catch {
    Write-Host "Connection failed. Please sign in to the popup window." -ForegroundColor Red
    exit
}

This step connects you to Exchange Online, ensuring you can manage the mailboxes effectively. If the connection fails, you'll receive a message prompting you to resolve the issue.

Step 3: Gather User Inputs

$user = Read-Host "Enter user UPN/email (e.g., username@domain.com)"
$roomFilter = Read-Host "Room name filter (e.g., Paris) or Enter for all"
$domainFilter = Read-Host "Domain filter (e.g., domain.com) or Enter for all"

In this step, you’ll be prompted to enter the user’s email and optional filters for room names and domains, allowing for more targeted mailbox management.

Step 4: Fetch Room Mailboxes

Write-Host "Searching for rooms... (this may take a moment)" -ForegroundColor Gray

try {
    $rooms = Get-EXOMailbox -RecipientTypeDetails RoomMailbox -ResultSize Unlimited -ErrorAction Stop
}
catch {
    Write-Host "✖ Error fetching rooms: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "Try running: Disconnect-ExchangeOnline and restart PowerShell." -ForegroundColor Yellow
    exit
}

This command retrieves all room mailboxes available in your organization. If there’s an error in fetching data, you'll receive an informative message to aid in troubleshooting.

Step 5: Apply Filters to Room Mailboxes

if ($roomFilter) {
    $rooms = $rooms | Where-Object { $_ .DisplayName -like "*$roomFilter*" }
}
if ($domainFilter) {
    $rooms = $rooms | Where-Object { $_ .PrimarySmtpAddress -like "*@$domainFilter" }
}

This filtering step narrows down the room mailboxes based on your input, making it easier to manage only the relevant rooms.

Step 6: Display Available Rooms

Write-Host " `nAvailable Rooms:`n" -ForegroundColor Cyan
$roomList = @()
$i = 1
foreach ($r in $rooms) {
    Write-Host "$i - $($r.DisplayName) [$($r.PrimarySmtpAddress)]"
    $roomList += $r
    $i++
}

This code lists all available room mailboxes for the admin to review. Clear output is important for usability and prevents errors in selection.

Step 7: User Selection of Rooms

$choice = Read-Host " `nChoose (A) for All or (S) for Specific"

if ($choice.ToUpper() -eq "S") {
    $selection = Read-Host "Enter numbers (e.g., 1,3,5)"
    $indexes = $selection -split "," | ForEach-Object { $_ .Trim() }
    $selectedRooms = @()
    foreach ($idx in $indexes) {
        if ($idx -match "^\d+$" -and [int]$idx -le $roomList.Count) {
            $selectedRooms += $roomList[[int]$idx - 1]
        }
    }
} else {
    $selectedRooms = $roomList
}

This interactive segment allows you to choose between applying permissions to all discovered room mailboxes or only selected ones, greatly enhancing flexibility.

Step 8: Apply Send-On-Behalf Permissions

Write-Host " `nApplying 'Send on Behalf' for: $user" -ForegroundColor Cyan

foreach ($room in $selectedRooms) {
    try {
        Set-Mailbox -Identity $room.ExternalDirectoryObjectId -GrantSendOnBehalfTo @{Add=$user} -ErrorAction Stop
        Write-Host "✔ Done: $($room.DisplayName)" -ForegroundColor Green
    }
    catch {
        Write-Host "✖ Error on $($room.DisplayName): $($_.Exception.Message)" -ForegroundColor Red
    }
}

This process applies the send-on-behalf permission to the selected room mailboxes. Clear feedback on success or failure keeps users informed.

Step 9: Adding Calendar Rights

# Replace with actual emails
Add-MailboxFolderPermission -Identity "user@d.com:\Calendrier" -User "userb@d.com" -AccessRights Reviewer
# Check current permissions
Get-MailboxFolderPermission -Identity "user@d.com:\Calendrier" -User "userb@d.com"

In this final step, you can add calendar access rights to a specific mailbox folder. This is crucial for allowing the user to view and manage calendar events appropriately.

Conclusion

By following these steps, you can effectively automate the management of Teams calendars within Outlook using PowerShell. This method simplifies the process of granting permissions and allows for better organization of scheduling resources—an indispensable tool for any IT admin. Automation not only saves time but also reduces the potential for human error, making your Microsoft 365 administration tasks smoother.

Key Takeaways

  • Automation streamlines calendar management significantly.
  • PowerShell provides a robust way to interact with Exchange Online mailboxes.
  • Clear messaging during the script execution aids troubleshooting.

🎓 Ready to go deeper?

Practice real MD-102 exam questions, get AI feedback on your weak areas, and fast-track your Intune certification.

Start Free Practice → Book a Session
Souhaiel Morhag
Souhaiel Morhag
Microsoft Endpoint & Modern Workplace Engineer

Souhaiel Morhag is a Microsoft Intune and endpoint management specialist with hands-on experience deploying and securing enterprise environments across Microsoft 365. He founded MSEndpoint.com to share practical, real-world guides for IT admins navigating Microsoft technologies — and built the MSEndpoint Academy at app.msendpoint.com/academy, a dedicated learning platform for professionals preparing for the MD-102 (Microsoft 365 Endpoint Administrator) certification. Through in-depth articles and AI-powered practice exams, Souhaiel helps IT teams move faster and certify with confidence.

Related Articles

Popular on MSEndpoint