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.”
Import-Module: The specified module 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”.

Get-module -ListAvailable

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.

  1. 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
  2. Pick “PSModulePath” and click on the “Edit” button.
    add path to PSModulePath variable
  3. 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.add to psmodulepath
  4. Restart PowerShell ISE or PowerShell console.

This should solve the problem.

Conclusion

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.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

4 thoughts on “Fix “Import-Module: The specified module ‘Module Name’ was not loaded because no valid module file was found in any module directory” Error

  • This does not work for me. Setting the path variable does not change the result. I am sure I am doing something else wrong.

    Reply
  • 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.”

    Reply
  • Thanks man, you really saved me a lot of trouble. Ive been battling this since yesterday.

    Reply
  • Thanks! This works perfectly and you helped me a lot with this post.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *