Can't import SharePoint online Powershell module

Copper Contributor

when I use
Import-Module Microsoft.Online.SharePoint.PowerShell

it says

PS C:\Windows\system32> Import-Module Microsoft.Online.SharePoint.PowerShell
Import-Module : Could not load type 'Microsoft.SharePoint.Administration.DesignPackageType' from assembly 'Microsoft.SharePoint.Client, Version=16.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c'.
At line:1 char:1
+ Import-Module Microsoft.Online.SharePoint.PowerShell
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Import-Module], TypeLoadException
+ FullyQualifiedErrorId : System.TypeLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand

 

I have this binary module installed:

Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Binary 16.0.86... Microsoft.Online.SharePoint.Powe...
Binary 16.0.80... Microsoft.Online.SharePoint.Powe...

 

Any clues?

24 Replies
Some things you can try:
(1) Check If you already installed the module by means of the SharePoint Online Management Shell...if so, uninstall it
(2) Try to us the -force parameter in the Import-Module cmdlet
I had the exact same problem, mine was caused by Microsoft.SharePoint.Client.dll installed in the Global Assembly Cache.

To resolve the issue,
(1) Navigate to C:\Windows\Microsoft.NET\assembly\GAC_MSIL
(2) Remove the Microsoft.SharePoint.* assemblies
(3) Uninstall the module with Uninstall-Module -Name Microsoft.Online.SharePoint.PowerShell

After reinstalling the module from the PowerShell gallery, the module worked flawlessly.


 I just found this thread because I was having the same problem - thank you for these instructions @Glenn Goffin, they did the trick for me! :smile:

Thx @Glenn Goffin , this one worked for me !

@Glenn Goffin 

 

I have very same error/issue trying to load on a SharePoint 2019 server.  I don't think it would be a good idea to delete all the Microsoft.SharePoint assemblies from this server.   Any ideas if it will corrupt the SharePoint installation? 

Thanks for your help! It worked for me.

@Glenn Goffin Thanks for your help! It worked for me.

What are those assemblies for in C:\Windows\Microsoft.NET\assembly\GAC_MSIL ?

@Glenn Goffin I don't think direct manipulation of dlls in the GAC is the right solution here. This issue is actually caused by a conflict with SharePoint Online Components SDK. Recent versions of the Microsoft.Online.SharePoint.PowerShell module simply will not load as long as that is installed on a system. If you uninstall SharePoint Online Components, the module will load just fine.

@Allison Bates 

would be also very interesting for me whether "clearing" the GAC destroys SharePoint Server installation.

@Kirk Munro although I strongly agree with your cautious approach to this, I wanted to note that my various attempts to uninstall the SharePoint Online Components (e.g. initial uninstall = no luck.  re-install and uninstall using elevated command prompt = no luck) never did the trick.  Using @Glenn Goffin 's GAC "nuclear option" was what finally did the trick for me.

 

I assume the SOC components came in from my Visual Studio 2019 install since I don't know how else they got installed, btw.  But for now, adios SOC/COSM dlls.

 

So, Kirk, your warnings may be especially important for those (unlike me) who are doing ShPt dev work.

 

Respectfully, D.

Thank you worked.

Manual removing CSOM assemblies from the GAC is not a good idea - especially if you have SP on-prem running on the same server. Problem is caused by several instances of SPO SDKs and may be solved this way:

1. Open Control Panel -> UnInstall Programs -> Search for SharePoint related setups.
2. Locate SharePoint Client Components -> Uninstall.
3. Locate SharePoint Online Management Shell -> Uninstall.
4. Ensure that anyone of the above setup is installed more than one time. If so, remove all the instances.
5. Now, open the Powershell console in administrative mode (Run as administrator).
6. Uninstall SPO Powershell module (If already exists) by running this command: Uninstall-Module -Name Microsoft.Online.SharePoint.PowerShell -AllVersions -Force
7. Finally, install the latest SPO Powershell module by running this command: Install-Module Microsoft.Online.SharePoint.PowerShell -Force

Thank you so much for your post. It has just worked for me after a lot of getting annoyed at powershell!
@Glenn Goffin 

@Alexey Sadomov these steps worked for me. Thanks!

@Glenn Goffin 

Hello everybody,
I had the same problem and was able to fix it with the manual, so my scripts all work fine, thanks for that.
But when I execute the script as a scheduled task I get this message again.

 

PS>TerminatingError(Import-Module): "Could not load type 'Microsoft.SharePoint.Administration.DesignPackageType' from assembly 'Microsoft.SharePoint.Client, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'."
Import-Module : Could not load type 'Microsoft.SharePoint.Administration.DesignPackageType' from assembly 
'Microsoft.SharePoint.Client, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'.
At C:\Users\xxx\Desktop\Skripte\Create-TECTeams.ps1:71 char:1
+ Import-Module -Name Microsoft.Online.SharePoint.PowerShell
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Import-Module], TypeLoadException
+ FullyQualifiedErrorId : System.TypeLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand
PS>TerminatingError(Import-Module): "Could not load type 'Microsoft.SharePoint.Administration.DesignPackageType' from assembly 'Microsoft.SharePoint.Client, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'."

 

I don't know what to do, if someone has an idea... I would be glad about every help.

best regards
Kai

Saved my life here.
Thanks for this great help !!!!!

 

@Andy_Bond and anyone else interested.

I have done several days searching for a solution and believe I have come across a few causes and solutions.

The obvious one is multiple versions of the module installed. The obvious is to uninstall all versions and install the latest either using Install-Module or obtaining the MSI from Microsoft. Very annoying but it does ensure that you have a reasonably clean environment. Along with uninstalling all versions is to follow the instructions provided by @Glenn Goffin unless, of course, you are doing this on a SharePoint server. Do not delete those directories from your SharePoint server!

Another interesting one surfaced where the error was being generated within the Exchange Management Shell (EMS) but didn't happen with the standard PowerShell window.
We onboard hundreds of accounts a day and part of the onboarding includes OneDrive test and request. The EMS, it turned out, this was our issue. As a result, I ended up writing the following function to replicate the EMS in a normal PowerShell window. Please read through...

if (!($exscripts -and $ExPath)) {
	$path = "HKLM:\Software\Microsoft\ExchangeServer"
	$keys = Get-ChildItem -Path $path -Recurse

	ForEach($Key in $Keys) {
		ForEach ($Value in (Get-ItemProperty -Path $key.PSPath -Name "MsiInstall*")){
			if ($Value.MsiInstallPath -match "Exchange" -and $Value.MsiInstallPath -match "bin") {
				$ExPath = $Value.MsiInstallPath
			}

		}
	}
	if (Test-Path $ExPath) {
		$ExPath = "$ExPath\RemoteExchange.ps1"
		. $ExPath
	}
	else {
		Write-Host "Exchange tools are not installed on this server or are installed in a different path" -ForegroundColor Yellow
		"Check the tools are installed and update the path in $($MyInvocation.MyCommand.Name) and search for '$ExPath'"
		Exit-Script
	}
}

$EXSession = Get-PSSession | Where-Object {$_.ComputerName -match "Partial_HostName"} ## We have an exchange cluster with incremental naming convention

if (!($EXSession)) {
	"Connecting to Exchange"
	Connect-ExchangeServer -Auto -AllowClobber
}

Obviously, this will only work if you have the Exchange tools installed on the host running the script.

The trick is to load the SharePoint Online modules first and then the EMS code above. So far, in the last 4 hours and the script running every 5 minutes, there has not been an error.

To note, we are running a hybrid Exchange environment so I am not sure if the issue is present when the Exchange Online tools are loaded before SharePoint Online as I moved SharePoint to be the first module loaded.

This worked for me. Thanks!

@Glenn Goffin  you help me too with this steps

Thanks!!!