You may receive an error when trying to run a cmdlet from a PowerShell module installed on your computer.
The command XXX was found in the module, but the module YYY could not be loaded.
I received this error while trying to connect to a Microsoft 365 tenant via the Exchange Online PowerShell module.
Connect-ExchangeOnline : The 'Connect-ExchangeOnline' command was found in the module 'ExchangeOnlineManagement', but the module could not be loaded. For more information, run 'Import-Module ExchangeOnlineManagement' + CategoryInfo : ObjectNotFound: (Connect-ExchangeOnline:String) [], CommandNotFoundException + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
This error typically indicates that your computer’s PowerShell Execution Policy settings are preventing third-party modules from launching.
Run the command:
Get-ExecutionPolicy
The Restricted policy setting prevents the execution of third-party scripts.
Use the following command to load the PowerShell module:
Import-Module ExchangeOnlineManagement
An error should appear:
Import-Module: File C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.5.0\netFramework\ExchangeOnlineManagement.psm1 cannot be loaded because running scripts is disabled on this system.
You can allow commands from external PowerShell modules to be run only in the current session.
Set-ExecutionPolicy RemoteSigned -Scope Process
Or, you can enable the execution of local scripts for the current user.
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Attempting to import a module may result in an error:
Import-Module : Could not load file or assembly or one of its dependencies. The system cannot find the file specified.
In this case, the module is most likely either not fully downloaded or corrupted. Delete the directory with the module from the local drive and then reinstall it. PowerShell modules are installed in one of the following directories:
C:\Users\%username%\Documents\WindowsPowerShell\Modules
– in the current user profileC:\Program Files\WindowsPowerShell\Modules
– third-party modules that are available to all users are usually installed here.C:\Windows\system32\WindowsPowerShell\v1.0\Modules
– built-in Windows modules
Here’s another example of the PS module import error:
Import-Module : File \modulename.psm1 cannot be loaded. The file \modulename.psm1 is not digitally signed. You cannot run this script on the current system.
This error may occur due to the PowerShell Execution Policy settings, or because the PSM1 module file was downloaded manually from the Internet. In this case, use the following command to unblock the file downloaded from the Internet:
Unblock-File .\modulename.psm1