Forum Discussion
Andy_Bond
Mar 03, 2019Copper Contributor
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...
KevinPinel
Feb 11, 2021Brass 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.