This article explains how to resolve “Import-Module: Assembly with same name is already loaded” error in PowerShell (and a couple of related issues – old versions of the module being used, updating modules silently failing, uninstalling modules simply not doing anything…)
Table of Contents
Different error messages you might get (obviously the same issue affects other modules and not just PnP PowerShell, but it’s the one I use often and the one that I ran into issues with.
Obviously, you might run into definitely valid and existing methods that aren’t loaded because an assembly with the same name is already loaded (even though it shouldn’t):
The 'Connect-PnPOnline' command was found in the module 'PnP.PowerShell', but the module could not be loaded due to the following error: [Assembly with same name is already loaded] For more information, run 'Import-Module PnP.PowerShell'.
Or you might run into new-ish commandlets that you KNOW exist, but that refuse to be loaded. Like the somewhat recent Invoke-PnPGraphMethod:
The term 'Invoke-PnPGraphMethod' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
You can confirm it is indeed a module loading conflict by making doubly sure the right module – in my case, PnP.PowerShell – is indeed loaded. Somewhat like below:
Import-Module -Name "PnP.PowerShell" Import-Module: Assembly with same name is already loaded
If your error looks about the same, you know it is the same issue too :)
I’ve found this to be caused by an older version of the module – again, in my case PnP PowerShell – being installed and loaded. You could try updating the module – because in theory, that is what solves the issue.
PS > update-module PnP.PowerShell -force PS >
But the surprise? This doesn’t update the module if it has been installed in any other than way than PowerShellGet (which is what Update-Module uses). Or, I presume, it is installed in a different folder because you have multiple PowerShell versions and some just install modules in different folders. But somehow still load them from each and cause an unsolvable conflict.
You’re better off uninstalling the earlier versions. You can do this like so:
# Uninstall all versions of the PnP PowerShell Uninstall-Module -Name PnP.PowerShell -AllVersions -Force # Install PnP PowerShell module Install-Module -Name PnP.PowerShell
Nice and easy! But the kicker? This doesn’t work either.
When you then try to install a completely NEW version of the module after seemingly uninstalling all of the earlier versions, you get an error like this:
WARNING: Version '1.11.0' of module 'PnP.PowerShell' is already installed at 'C:\Program Files\WindowsPowerShell\Modules\PnP.PowerShell\1.11.0'. To install version '2.1.1', run Install-Module and add the -Force parameter, this command will install version '2.1.1' side-by-side with version '1.11.0'.
Ah. So despite all of our trouble force-uninstalling the module, PowerShell still thinks it exists. And right it is.
So Uninstall-Module -AllVersions -Force does not uninstall all versions. It definitely doesn’t force uninstalling them.
Time needed: 15 minutes.
How to actually uninstall conflicting PowerShell modules?
- Figure out where your commandlets are installed
First, you need to figure out where your conflicting module is installed. In theory, you can get all of the module installation paths from an environment variable like this:
But this might return a lot of paths, like below:
C:\Users\Kalle Pettersson\OneDrive - Ridiculous OneDrive Path Because Why Not\Dökuments\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\powershell\7\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\Microsoft SQL Server\150\Tools\PowerShell\Modules\
Yes – it might contain a lot of paths, including abominations like localized OneDrive paths.
How do you get just the one, offending module’s path, then?
- Figure out where your conflicting module is installed
If you know your Commandlet’s name, you can of course just fetch it and output the path like this:
(Get-Module -ListAvailable Pnp.*).path
And you should get something like this:
C:\Users\Kalle Pettersson\OneDrive - Ridiculous OneDrive Path Because Why Not\Dökuments\PowerShell\Modules\PnP.PowerShell\1.10.0\PnP.PowerShell.psd1
Great! Now we know where to go.
- Navigate to the folder and remove the files
Now you can simply navigate to the offending folder and remove the subdirectories. While PowerShell might not be able to remove them, you should be.
- Install the new version of the Module
You should be good to install your desired version of the commandlet now! Something like below should now work:
Install-Module -Name PnP.PowerShell
That should be pretty much it. Did it work for you? Let me know in the comments below!
- “This app can’t run on your PC” dialog when you’re building your app in Visual Studio - September 12, 2023
- How to “fix” Word removing tagged users from comments? - September 5, 2023
- How to install Home Assistant on Synology NAS (DSM 7.2)? - August 29, 2023