Forum Discussion
Can't import SharePoint online Powershell module
Andy_Bond, It took me a full day to come to this work around. Thanks for your question and @Glenn's response (which by the way, didn't work in my case)
Main Error:
Connect-SPOService : The 'Connect-SPOService' command was found in the module 'Microsoft.Online.SharePoint.PowerShell', but the module could not be loaded. For more
information, run 'Import-Module Microsoft.Online.SharePoint.PowerShell'.
Sub-Error:
Import-Module : Could not load type 'Microsoft.SharePoint.Client.CustomerRecoveryKeyMode' from assembly 'Microsoft.SharePoint.Client, Version=16.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c'.
Solution:
If you get this error while trying to use Connect-SPOSite command in your script, then close the PowerShell Session completely
Uninstall module Microsoft.Online.ShareP¬oint.PowerShell and install it again, then import it again
If you have trouble importing (Error: Can’t import), then use Glenn’s solution and it shall work and says: “Successfully imported” (even with Client Component SDK installed but just those specific files are removed)
{
Glenn's solution:
(1) Navigate to C:\Windows\Microsoft.NET\assembly\GAC_MSIL
(2) Remove the Microsoft.SharePoint.* assemblies
}
After successfully importing, you must not put those removed files back in their place, you’ll get the same error (tried it)
Now, to avoid encountering the same error again while you run that script the next time,
keep these in mind:
1) PowerShell Session means when you freshly open PowerShell ISE window, session ends when you completely close PowerShell ISE Window
2) Whenever you open a new PowerShell Session, BEFORE running anything related to loading CSOM assemblies & $ctx client context, ctx.execute query and all of that ClientComponent related stuff, just Run the ConnectSPO-Site Command once, and then do whatever you want with whatever scripts you have
Enjoy! (and you’re Welcome😊)