0

Explanation

In PowerShell command Get-Module -ListAvailable should give you (MS Docs link) all modules in paths listed in the $PSModulePath environment variable. But that is not full story. What I figured out so far is that directory name in which module manifest is located must be the same as module name. It can optionally include version as additional folder level.

For example these modules are returned:

C:\Program Files\WindowsPowerShell\Modules\Azure.Storage\4.6.0\Azure.Storage.psd1
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\NetTCPIP\NetTCPIP.psd1

Even that is not complete story. I have these modules on my computer, located in directory list in $PSModulePath, but they do not appear in Get-Module -ListAvailable results.

But, these module manifests are not listed as avaialable modules:

C:\Users\iiric\Documents\PowerShell\Modules\Az.KeyVault\2.1.0\SecretManagementExtension\SecretManagementExtension.psd1
C:\Program Files\WindowsPowerShell\Modules\Azure\5.1.2\ExpressRoute\ExpressRoute.psd1
C:\Windows\System32\WindowsPowerShell\v1.0\Modules\AppLocker\AppLocker.psd1

Note that this is not related to paths. I am getting back module Az.KeyVault version 2.1.0 just fine.

Question

How exactly module discovery is working in PowerShell? Is it described somewhere in more technical details?

Additional hints

Based on source code analysis, I saw some kind of results filtering based on edition and other properties.

Also, what I am suspecting is that Get-Module once it finds some module, it is not searching inside its subdirectories. However, I was not able to find this documented.

PSModulePath

PS > $env:PSModulePath -split ';'

C:\Users\iiric\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\7\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\
C:\Program Files (x86)\Windows Kits\10\Microsoft Application Virtualization\Sequencer\AppvPkgConverter
C:\Program Files (x86)\Windows Kits\10\Microsoft Application Virtualization\Sequencer\AppvSequencer
C:\Program Files (x86)\Microsoft Azure Information Protection\Powershell
c:\Users\iiric\.vscode-insiders\extensions\ms-vscode.powershell-2020.6.0\modules
Igor
  • 1,349
  • 12
  • 25
  • Can you paste the output from `$env:PSModulePath -split ';'` into your post? – Mathias R. Jessen Nov 05 '20 at 20:21
  • I have added requested info. – Igor Nov 05 '20 at 20:25
  • And added some more info on paths. – Igor Nov 05 '20 at 20:31
  • 1
    Looks like you just answered your own question, the paths to the folders containing the `ExpressRoute` and `Az.KeyVault` secrets management extension modules are not in the `PSModulePath` env variable. `AppLocker` likely doesn't show up because it can't be loaded by PowerShell 7 (I wouldn't be surprised if it has a hard dependency on a .NET Framework library) – Mathias R. Jessen Nov 05 '20 at 20:33
  • Hm, this one is found `C:\Users\iiric\Documents\PowerShell\Modules\Az.KeyVault\2.1.0\Az.KeyVault.psd1` but, this one is not `C:\Users\iiric\Documents\PowerShell\Modules\Az.KeyVault\2.1.0\SecretManagementExtension\SecretManagementExtension.psd1` Technically, both of them are under `C:\Users\iiric\Documents\PowerShell\Modules` – Igor Nov 05 '20 at 20:39
  • And, just to be clear. I do have a list of 46 different module manifests under $PSModulePaths which are **not** listed with `Get-Module`. I am not looking for explanation on these exact modules, but more like some general overview and preferably some public documentation on it. – Igor Nov 05 '20 at 20:44
  • 2
    [This is probably the best explanation I'm aware of](https://www.youtube.com/watch?v=jWXe-xnpLsI) – Mathias R. Jessen Nov 05 '20 at 20:46
  • I think this article will help you, https://stackoverflow.com/questions/51207167/how-does-powershell-know-where-to-find-modules-for-import – Shaqil Ismail Nov 06 '20 at 18:38

0 Answers0