Fix "Import-Module: The specified module was not loaded because no valid module file was found in any module directory" Error

Problem: When trying to import the PnP PowerShell module, got an error message "Import-Module: The specified module 'SharePointPnPPowerShellOnline' 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 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.

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:
As far next step, We've to verify if our custom module's path is added in the "PSModulePath" variable.
  • Go to System Properties (Windows Key + Pause Break) >> Click on Advanced System Settings >> Click on Environment Variables
  • Pick "PSModulePath" and click on "Edit" button.
    add path to PSModulePath variable
  • Add the the path to this variable. In my case, its "C:\Program Files\WindowsPowerShell\Modules"
  • Restart PowerShell ISE or PowerShell console.
This should solve the problem.

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.