Forum Discussion

Andy_Bond's avatar
Andy_Bond
Copper Contributor
Mar 03, 2019

Can't import SharePoint online Powershell module

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?

27 Replies

  • CloudSnout's avatar
    CloudSnout
    Copper Contributor
    Great to see this issue still isn't resolved in 2024, version 16.0.25012.12000
    For each different job you would need a different SharePoint module, which apparantly don't play well together on the same system.
    • KevinPinel's avatar
      KevinPinel
      Brass Contributor
      I have long left this module and working with PnP.PowerShell. It's not exactly intuitive and has some of its own issues but for the most part it is working.
  • 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.

    • KevinPinel's avatar
      KevinPinel
      Brass Contributor

      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
      ```
  • Mirza1443's avatar
    Mirza1443
    Copper Contributor

    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😊)

     

  • KevinPinel's avatar
    KevinPinel
    Brass Contributor

    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.

  • Alexey Sadomov's avatar
    Alexey Sadomov
    Copper Contributor

    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

  • Glenn Goffin's avatar
    Glenn Goffin
    Brass Contributor
    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.


    • Mirza1443's avatar
      Mirza1443
      Copper Contributor
      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
  • 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

Resources