Several PowerShell 5.1 scripts that manage users, mail, and distribution groups through Exchange Online and Microsoft Graph started failing recently. The failure occurs at Connect-MgGraph with a TypeLoadException stating that RefreshCacheAsync in Microsoft.Graph.PowerShell.Authentication.Core.TokenCache.InMemoryTokenCacheOptions does not have an implementation. Send-MgUserMail then fails because authentication was never established. The app registrations, certificates, client and tenant IDs, and service account all appear valid. The scripts are running on Windows PowerShell 5.1 with Microsoft.Graph modules version 2.40.0, and they also load Exchange-related modules. Has anyone encountered this compatibility issue, and what is the best fix?
3 Answers
This looks like a module compatibility problem rather than an expired credential. The 2.40.0 Graph update appears to conflict with other PowerShell modules, especially when Exchange Online modules are loaded in the same session. Try installing and explicitly importing the previous Graph versions, such as 2.39.0, for the authentication and mail modules: `Install-Module -RequiredVersion 2.39.0 Microsoft.Graph.Authentication,Microsoft.Graph.Mail` followed by `Import-Module -RequiredVersion 2.39.0 ...`. Pinning the versions in the script can also prevent an automatic update from breaking it again.
Version 2.40.0 was released shortly before these failures appeared, so checking the module release notes or reporting it to the SDK maintainers would be worthwhile. As a temporary workaround, you can call the Graph REST endpoint directly with Invoke-RestMethod, although reverting to the last known-good SDK version is usually simpler for existing scripts.
Try running Connect-MgGraph by itself outside the full script. If it works in a clean PowerShell session but fails after the Exchange modules are imported, the assemblies are colliding. Splitting the work into separate scripts or sessions so each one loads only the required administration module can avoid the conflict. A clean session also helps confirm whether the issue is with authentication or with incompatible loaded assemblies.

The scripts do load the Exchange modules as well. I’m going to test rolling the Graph modules back and pinning the versions.