/errors/powershell/module-could-not-be-loaded
PowerShell: The 'x' command was found in the module 'y', but the module could not be loaded.
- tool
- PowerShell
- exit code
- 1
- versions
- Windows PowerShell 5.1, PowerShell 7.x
- os
- windows, linux, macos
The 'Get-Thing' command was found in the module 'Thing', but the module could not be loaded. For more information, run 'Import-Module Thing'. comes from module auto-loading: PowerShell found the cmdlet in a module on $env:PSModulePath, tried to import it automatically, and the import failed. The message hides the real reason. Run the Import-Module command it suggests; the error from that command (execution policy, missing file, assembly conflict) is what to fix.
Reproduce it
A small module whose .psm1 fails while loading, placed on PSModulePath for this session:
$ErrorActionPreference = 'Stop'
New-Item -ItemType Directory -Path mods\Broken | Out-Null
Set-Content mods\Broken\Broken.psm1 'throw "Dependency Contoso.Core.dll not found"; function Get-BrokenThing { "x" }'
New-ModuleManifest -Path mods\Broken\Broken.psd1 -RootModule Broken.psm1 -FunctionsToExport Get-BrokenThing -ModuleVersion 1.0.0
$env:PSModulePath = (Join-Path $PWD 'mods') + [IO.Path]::PathSeparator + $env:PSModulePath
try { Get-BrokenThing } catch { $_.Exception.Message; ' -> ' + $_.FullyQualifiedErrorId }
try { Import-Module Broken } catch { 'Import-Module says: ' + $_.Exception.Message }Output
The 'Get-BrokenThing' command was found in the module 'Broken', but the module could not be loaded. For more information, run 'Import-Module Broken'. -> CouldNotAutoloadMatchingModule Import-Module says: Dependency Contoso.Core.dll not found
The first message only says the import failed; Import-Module shows the error thrown by the module.
Typical underlying errors
| Import-Module shows | Fix |
|---|---|
| ... cannot be loaded because running scripts is disabled on this system | Set-ExecutionPolicy -Scope CurrentUser RemoteSigned |
| ... is not digitally signed | Unblock-File the module files (Get-ChildItem -Recurse | Unblock-File) |
| Could not load file or assembly ... Assembly with same name is already loaded | new session; import the module before the one that loaded the other version |
| The required module ... is not loaded / not installed | install the dependency module listed in RequiredModules |
| ... is not compatible with PowerShell Core (or similar edition message) | use the edition the module supports, or Import-Module -UseWindowsPowerShell in PowerShell 7 |
Check what is installed
Get-Module -ListAvailable Thing | Select-Object Name, Version, ModuleBase
Import-Module Thing -VerboseSeveral versions in different folders of PSModulePath are a common cause of confusion: auto-loading picks the highest version, which may not be the one you fixed.
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/import-module
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_windows_powershell_compatibility