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

Solution:

This error is because the PowerShell console or ISE is unable to get the module from given module path’s in the environment variable “PSModulePath”. To fix 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. To get the modules installed, Open 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 have to 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 “Edit” button.
    add path to PSModulePath variable
  • 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.add to psmodulepath
  • Restart PowerShell ISE or PowerShell console.

This should solve the problem.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time 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 *