ERROR: Could not load type ‘Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider’ from assembly ‘Microsoft.Graph.Core

After upgrading Microsoft Graph, I noticed an issue when trying to run cmdlet Get-MgGroup or Get-MgUser. I could connect to Graph with no issues, but multiple cmdlets failed.

Error

Get-MgGroup : Could not load type 'Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider' from assembly 'Microsoft.Graph.Core, Version=1.25
.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
At line:1 char:1
+ Get-MgGroup -All
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-MgGroup_List], TypeLoadException
    + FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Cmdlets.GetMgGroup_List

This command did also fail

[Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider]
CategoryInfo : InvalidOperation: (Microsoft.Graph…ssTokenProvider:TypeName) [], RuntimeException

FullyQualifiedErrorId : TypeNotFound

Root Cause

Even though Microsoft Graph v2.2 installed the version 3.0.9.0 of Microsoft.Graph.Core.Dll, there were multiple versions of Microsoft.Graph.Core.dll found in the $Env:PsModulePath path.

A search found multiple older versions (1.11, 1.24, 1.25) of Microsoft.Graph.Core.dll related to PnP.Powershell module.

Resolution

Remove older versions of Pnp.Powershell (or other Powershell-modules having old DLLs)

uninstall-module -Name PnP.PowerShell -AllVersions

Issue: Uninstall – Module x is currently in use ….

If you see the below error, it is caused by a dependent DLL being loaded as part of the shell / session and therefore in use.

Solution: Module x is currently in use ….

Method 1: Remove-Module + Uninstall-Module

Run the Remove-Module first to unload the imported module from memory.

After this you can do an uninstall-module

Remove-Module -Name <ModuleName> -force
Uninstall-Module -Name <ModuleName> -force

Method 2: Start a CMD and then a Powershell with -NoProfile

(1) Close all running Powershell sessions

(2) Start a CMD as local admin

(3) Run the below Powershell-command with the -NoProfile and -NonInteractive. Powershell will now be started, without loading any DLLs into the session.

C:\>powershell -NoProfile -NonInteractive -Command "uninstall-module -Name PnP.PowerShell -AllVersions"

9 thoughts on “ERROR: Could not load type ‘Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider’ from assembly ‘Microsoft.Graph.Core”

  1. Hello
    In Azure Automation using pnp 2.4.0 module this bug still appear in combination with graph. What can I do?

    Reply
  2. Thank you!
    Was having this issue on my VS Code. I have the VS Code System Installer.
    I uninstalled PnP.PowerShell v2.4.0 and installed v2.2.0 instead as it has no issues with that.
    It does also have issues with v2.3.0

    Uninstall-Module -Name PnP.PowerShell -Force
    Install-Module -Name PnP.PowerShell -Force -Scope AllUsers -RequiredVersion 2.2.0

    Reply
  3. For others who experience the same issue. I was having the same problem using the latest versions of PNP.PowerShell (2.10) and Graph (2.22) in a runbook (Automation Account). I fixed it by executing dummy cmdlet right after connecting to PNP ($test = Get-MgUser -top 10). So it looked like that: Connect-MgGraph, $test = Get-MgUser -top 10, Connect-PnPOnline.
    Somehow it fixed the problem, because I don’t have any outdated modules and only a few graph ones.

    Reply

Leave a Reply