Fix conflicts between PnP.PowerShell and Microsoft.Graph.Authentication module

German post was automatically translated by DeepL and manually reviewed

If someone uses the two PowerShell modules PnP.PowerShell and Microsoft.Graph.Authentication in scripts, conflicts can occur repeatedly.

There are currently two known and often discussed conflicts. They are due to outdated DLL versions in the PnP.PowerShell module. And no, the order in which the modules are loaded does not play a role in my cases. In my case, I load Microsoft.Graph.Authentication and do not load the PnP.PowerShell module.

Conflict 1

Error “Microsoft.Graph.Core, Version=1.25.1.0” > I described the cause and solution in February; also see the GitHub discussion.


Conflict 2

Error “Could not load type ‘Microsoft.Identity.Client.BaseAbstractApplicationBuilder`1’ from assembly ‘Microsoft.Identity.Client, Version=4.50.0.0”

PowerShell

Similar to conflict 1, it loads an outdated file from the PnP.PowerShell module. In practice, Connect-MgGraph should use the file from its own module.

PowerShell
[System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object Location | Sort-Object -Property FullName | Select-Object -Property FullName, Location | where FullName -like "Microsoft.Identity.Client,*"

PowerShell

Checking the Microsoft.Identity.Client.dll file in the PnP.PowerShell module confirms the old version.

Veraltete Microsoft.Identity.Client.dll
Outdated Microsoft.Identity.Client.dll

The problem is described in GitHub. As a solution, someone mentions replacing the old DLL in the PnP.PowerShell module with the one from Microsoft.Graph.Authentication. Once the file has been replaced, Connect-MgGraph works again. Deleting the file is not advisable in this conflict. PnP.PowerShell needs the file.

I wrote the PowerShell script Resolve-TAPnPPowerShellConflicts for both conflicts. The script executes the mentioned solutions for both conflicts. Note the information in the command and my documentation.

  • For conflict 1, it deletes the file Microsoft.Graph.Core.dll from the PnP.PowerShell Directory. Connect-MgGraph then loads the correct DLL.
  • For conflict 2, it overwrites Microsoft.Identity.Client.dll in the PnP.PowerShell Directory with the version from Microsoft.Graph.Authentication. Connect-MgGraph can then establish the connection without error.
Microsoft.Identity.Client.dll wurde ersetzt
Microsoft.Identity.Client.dll has been replaced

Please note that after an update of the PnP.PowerShell module, the script must be executed again.
If unexpected issues occur with PnP.PowerShell due to the two changes, the issue can be solved easily: Update-Module PnP.PowerShell -Force
An update overwrites everything in the current PnP.PowerShell directory.

Share
Avatar photo

Tobias Asböck

Tobias is a Senior System Engineer with around ten years of professional experience with Microsoft 365 products such as SharePoint Online, OneDrive for Business, Teams Collaboration, Entra ID, Information Protection, Universal Print, and Microsoft 365 Licensing. He also has 15+ years of experience planning, administering, and operating SharePoint Server environments. Tobias is a PowerShell Scripter with certifications for Microsoft 365 products. In his spare time, Tobias is busy with updates in the Microsoft 365 world or on the road with his road bike and other sports activities. If you have additional questions, please contact me via LinkedIn or [email protected].

2 Responses

  1. Kevin Pinel says:

    Have some suggestions for your Resolve-TAPnPPowerShellConflicts.ps1
    $PnPIdentityClientFile = Get-Item -Path ($PnPModuleDirectory + “\Core\Microsoft.Identity.Client.dll”)
    # if( $PnPIdentityClientFile.VersionInfo.FileVersion -like “4.50.*” ) { ## replacing with a version comparison
    if ($MGIdentityClientFile.VersionInfo.FileVersion -gt $PnPIdentityClientFile.VersionInfo.FileVersion){ ## if Graph version is new than the the pnp one, replace the pnp file

    Write-Host “Copying Microsoft.Identity.Client.dll from Microsoft.Graph.Authentication to PnP.PowerShell module directory”

    # Try to copy the Microsoft.Identity.Client.dll file from the Microsoft.Graph.Authentication module to the PnP.PowerShell module
    try {
    Get-Item -Path ($MgGraphAuthModuleDirectory + “\Dependencies\Core\Microsoft.Identity.Client.dll”) | Copy-Item -Destination ($PnPModuleDirectory + “\Core”) -Force -ErrorAction Stop
    }
    catch {
    # If the file is in use and can’t be copied, display an error message
    Write-Host “Error copying Microsoft.Identity.Client.dll, file is in use. Please close any PowerShell sessions and try again.” -f Red
    }
    } else
    {
    # If the version of the file is not 4.5.*, display a message
    Write-Host “Skipped, Microsoft.Identity.Client.dll version is the same or newer version to one used in the Graph modules” -ForegroundColor Green
    }

    Still testing, but at the time of writing this, with PnP 2.12.0, the identity client.dll file is a newer version to the one provided with Graph. PnP 4.64.0.0 to Graph 4.61.3.0

    I currently have a session running graph and PnP without issue, but will welcome your more extensive knowledge and testing.

    • Thanks Kevin for the input. Good suggestion. I just use the version 4.50.* and lower because they have known conflicts.
      For now I found no comments about conflicts with 4.50.* and higher. As you may have seen, some users have problems when PnP.PowerShell cannot use their DLLs, for example with ListTemplates as described here. So I’m not sure if it’s always necessary to replace Microsoft.Identity.Client.dll with the version from Microsoft Graph.

Leave a Reply

Your email address will not be published. Required fields are marked *