Fix “Import-Module: The specified module ‘Module Name’ was not loaded because no valid module file was found in any module directory” Error
Problem: When trying to import the PnP PowerShell module, I got an error message “Import-Module: The specified module ‘SharePointPnPPowerShellOnline’ was not loaded because no valid module file was found in any module directory”.
You may face this trouble for other PowerShell modules as well. E.g.
- “import-module : the specified module ‘activedirectory’ was not loaded because no valid module file was found in any module directory.”
- “import-module : the specified module ‘exchangeonlinemanagement’ was not loaded because no valid module file was found in any module directory.”
- “import-module : the specified module ‘sqlserver’ was not loaded because no valid module file was found in any module directory.”
Root Cause:
The “Import-Module : No valid module file was found in any module directory” error occurs when PowerShell is unable to locate the module that you are trying to import.
Solution:
This error is because the PowerShell console or ISE is unable to get the module from the given module path’s in the environment variable “PSModulePath”. To resolve the issue, You have to make sure your custom module’s path is appended to the PSModulePath environment variable.
Step 1: Get the Path of your custom PowerShell Module.
Make sure you have the custom module installed already on your system. To get the modules installed, Open the PowerShell console and type:
Get-module -ListAvailable
This gets all installed modules in your system along with their installed Path. Get the path of your custom module as highlighted. In my case, the PnP PowerShell module was installed at “C:\Program Files\WindowsPowerShell\Modules”.
Step 2: Verify if the custom module’s path is added to the “PSModulePath”
As the next step, we must verify if our custom module’s path is added to the “PSModulePath” variable.
- Go to System Properties by pressing Windows Key + Pause Break (Or Go to File Explorer >> Right click on “This PC” >> Properties) >> Click on Advanced System Settings >> Click on Environment Variables
- Pick “PSModulePath” and click on the “Edit” button.
- Append your PowerShell Module path to this variable. In my case, it is “C:\Program Files\WindowsPowerShell\Modules”, separated by ; (semicolon). From Windows 10 onwards, we have a neat option to add a new path. Hit the “New” button and add the module’s path there.
- Restart PowerShell ISE or PowerShell console.
This should solve the problem.
In conclusion, the “Import-Module : No valid module file was found in any module directory” error can occur when PowerShell is unable to locate the module that you are trying to import. To fix this error, verify that the module is installed on your system, and check the module path. Additionally, you may need to check the version of the module and your PowerShell version to ensure compatibility. By following these solutions, you should be able to resolve the error and successfully import the module into PowerShell.
This does not work for me. Setting the path variable does not change the result. I am sure I am doing something else wrong.
I dont understand: “Append your PowerShell Module path to this variable. In my case, its “C:\Program Files\WindowsPowerShell\Modules”, separated by ; (semicolon). From Windows 10 onwards, we have a neat option to add new path. Hit “New” button and add the module’s path there.”
Thanks man, you really saved me a lot of trouble. Ive been battling this since yesterday.
Thanks! This works perfectly and you helped me a lot with this post.