[Solved] No valid module file was found in any module directory

Problem context

Everything seems correct:

  • The path is correctly set to find your PowerShell module (normally C:\Program
  • Your PowerShell module was correctly build
    • Your files are in a subfolder of the C:\Program Files\WindowsPowerShell\Modules folder
    • the PSM1 and PSD1 (if you created one) files have the same name as the folder name
  • Your module is working fine on servers later than Windows 2008 R2
  • However, on Windows 2008 R2 servers, you get the following error:
    Import-Module : The specified module 'pswindowsupdate' was not loaded because no valid module file was found in any module directory.
        + CategoryInfo          : ResourceUnavailable: (pswindowsupdate:String) [Import-Module], FileNotFoundException
        + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

The reason

I didn’t find out why, but the fact is that even if you install PowerShell 5.1 on Windows 2008 R2 server, they don’t
discover modules that have sub-versions in subfolders.
For instance, in the following screenshot, you can see four different valid versions of the PSWindowsUpdate module.

This works fine on Windows 2012 servers or later, but unfortunately not on Windows 2008 R2 servers.

The workaround

No solution, but only workarounds:

  • First workaround: Copy all the content of the version you want to use directly in the module’s main folder.
    In my example above, I copied all files from the folder directly in the PSWindowsUpdate
    folder and it works!
  • Another workaround: Import the module with the full path to the psd1 or psm1

            Import-Module 'C:\Program Files\WindowsPowerShell\Modules\PSWindowsUpdate\\PSWindowsUpdate.psd1'

More about

to Write a PowerShell Script Module (MSDN)

One thought on “[Solved] No valid module file was found in any module directory

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s