"Could not load file or assembly 'Microsoft.Identity.Client, Version=4.44.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae'." error whilst trying to use ExchangeOnlineManagement v3
Problem:
So, the scenario, you have installed powershell 7.3.4 (or just 7), you have decided you want to use the newest version of ExchangeOnline (version 3.1.0 at this time), and you want to use app-only authentication using certificates and a Azure app registration.
You think, lets run this commando to connect to exchange Online:
Connect-ExchangeOnline -Organization "company.onmicrosoft.com" -Certificate $cert -AppId $appId
But instead of connecting, you receive the following error:
"Could not load file or assembly 'Microsoft.Identity.Client, Version=4.44.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae'."
When you run it in Powershel 5.1, it works perfectly fine. But you don’t want to use powershell 5.1, beacause thats stupid.
Solution
And now for the solution, you are missing an assembly reference, and to fix that we need to download some stuff and fix some other stuff. So lets start with confirming if the Microsoft.Identity.Client is actually listed as an available assembly (module) in powershell 7. You can do this by running this command (ps, run everything as administrator using powershell 7, NOT 5!):
get-module -ListAvailable
You have to find the module Microsoft.Identity.Client in the output of that command. Tip: in visual studio, you can use CRTL+F to open a search bar, then you can just type “Microsoft.Identity.Client“ and see if you have any results.
If the module is not listed, then we need to install it. You can do this using this command:
Install-Module -Name Microsoft.Identity.Client -RequiredVersion 4.44.0.0
As you can see I have specified a specific version based on what the error told me is missing. This might change in the future so adjust your specific version based on the specific error you are given. The reason I dont just use the latest version is because the ExchangeOnlineModule wants this version specificly.
If the installation did not work, try adding the NuGet package repository to your powershell enviroment, you can do that with this instruction: https://learn.microsoft.com/en-us/powershell/module/packagemanagement/register-packagesource?view=powershellget-2.x , or just by executing this command: Install-Module NuGet
. Then try to rerun the commando again, but this time like this: Install-Module -Name Microsoft.Identity.Client -RequiredVersion 4.44.0.0 -providername NuGet
Now that we have installed the module, please close all open powershell 7 sessions. It might be wise to just reboot your computer now.
After we have done that, try rerunning Connect-ExchangeOnline -Organization "company.onmicrosoft.com" -Certificate $cert -AppId $appId again, if it works, then your done.
Tip:
If you are using the MSAL.PS powershell module, then you can't use the ExchangeOnlineManagement V3 module. This is because the MSAL.PS powershell module comes with its own version of Microsoft.Identity.Client V4.37.0.0. That one is no longer compatible with the ExchangeOnlineManagement V3 module (At least Microsoft.Identity.Client V4.44.0.0 is required for V3) . There will probably never be a fix for that, because the MSAL.PS module is deprecated (at least I thought so).
Extra tip:
If you want to use app-only authentication (so certificate based authentication) for exchangemodule V3, and you are also using the Microsoft.Graph.Authentication V1.27.0 module, then you are probably screwed just like I am now. Because the Microsoft.Graph.Authentication V1.27.0 still uses the System.IdentityModel.Jwt.Token V5.6.0.0 assembly, whilst ExchangeOnlineManagement requires System.IdentityModel.Jwt.Token V6.22.1.0. Im currently in contact about this with Microsoft, but for now, I don't have a solution. There is just a versioning conflict between the 2 assemblies, and both versions are required for the modules to work. If you want to stay in the loop about this one, here is a link to my report on this: https://learn.microsoft.com/en-us/answers/questions/1292220/can-t-use-exchangeonlinemanagement-v3-1-0-together
update on the last tip: I have submited the issue to the Microsoft Graph github page: https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/2042