Jussi Roine
Home About Me Contact
Home About Me Contact

Connecting to Exchange Online with Connect-ExchangeOnline is slow within Azure Function

August 13, 2023 Jussi Roine
azure powershell serverless
Connecting to Exchange Online with Connect-ExchangeOnline is slow within Azure Function

I’ve written previously about building an Azure Function that connects with Exchange Online. One of the reasons for this was the need to modify a given user’s proxyaddresses property.

One issue I’ve persistently hit is with Connect-ExchangeOnline. Running this via managed identity with the following syntax:

Connect-ExchangeOnline -ManagedIdentity -Organization $tenant

This would take up to 35 seconds to complete! The other issue was excess memory usage. Each call to an Azure Function consumed 300-500 MB of memory of the App Plan compute instance. After 3 or 4 calls, it would simply run out of memory. Remove-PSSession didn’t seem to clean up the Exchange Online session.

To fix this, the slow connect time can be mitigated by adding the CommandName parameter for Connect-ExchangeOnline. You’ll need to introduce only the cmdlets you plan on using, and this in turn, reduces the memory footprint and improves performance. See details here. The connection now happens with:

Connect-ExchangeOnline -ManagedIdentity -Organization $tenant -CommandName Set-Mailbox,Disconnect-ExchangeOnline

The other issue for excess memory usage has to do with a memory leak in Connect-ExchangeOnline. To clear up the session objects, use Disconnect-ExchangeOnline -Confirm:$false before Remove-PSSession. In addition, enforce garbage collection with [System.GC]::Collect().

With these changes, the Azure Function completes in 4 seconds (down from about 35-40 seconds), and the memory footprint stays under 300MB at all times.

I hope this helps anyone else struggling with slow Connect-ExchangeOnline issues!

Jussi Roine

Jussi Roine

Microsoft MVP and consultancy founder with 30+ years of experience, passionate about Microsoft security, AI governance, and sharing what I learn along the way.

Tags

azure productivity microsoft 365 homelab windows cloud computing software development security data management sharepoint networking automation microsoft teams cybersecurity certifications virtualization macos power platform ai and machine learning docker identity management power automate artificial intelligence azure openai devops c# powershell linux serverless containers
Jussi Roine

Microsoft MVP and consultancy founder with 30+ years of experience, passionate about Microsoft security, AI governance, and sharing what I learn along the way.

© 2026 Jussi Roine. All rights reserved.