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

@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:smiling_face_with_smiling_eyes:)

 

Just an additional thing I had to do after your solution, whenever I open a new Powershell session, I have to run a command like Connect-SPOSite before running anything related to loading CSOM assemblies, Ctx xlient context, and all that Client Component SDK related stuf. Took me a full day to realise that, a lot of trial and error, trying with different versions, etc

To work with SharePoint online, you need the SharePoint Online Management Shell PowerShell module. Once installed (on your Windows 10 machine), SharePoint Online PowerShell will be loaded automatically in the PowerShell console as well as ISE without importing the module manually.

You need to connect SPO like this:
Connect-SPOService -Url https://contoso-admin.sharepoint.com -Credential email address removed for privacy reasons

After -Credential type the SharePoint Online administrator email.

@syed_nasir_abbas I think you missed the point of the op's post.

The issue isn't the cmdlet or how to format it. The issue is that module often generates an error.

 

Also, as MS is removing basic authentication, your suggestion will not work after October. The preferred method is now. This will prompt for the web login and supports MFA

```

Connect-SPOService -url $Global:SPODomain -ModernAuth $true
```

@Glenn Goffin 

 

Thanks for your answer