Home > Enterprise >  How do I list all the references of a given PowerShell module?
How do I list all the references of a given PowerShell module?

Time:11-14

How to list all the modules that this given module depends upon? (Particularly, I need their names and versions)

CodePudding user response:

To find the formally declared module dependencies - if any - of a given module via its module manifest (*.psd1):

Get-Module $someModule -ListAvailable | ForEach-Object {
  $_.RequiredModules, $_.NestedModules.RequiredModules
}
  • Get-Module returns System.Management.Automation.PSModuleInfo instance(s) describing the specified module(s).

  • -ListAvailable makes sure that modules that aren't presently imported but are discoverable via auto-loading, are included in the lookup.

  • $_.RequiredModules lists the direct module dependencies, and $_.NestedModules.RequiredModules those of any nested modules, which is presumably rare.Tip of the hat to Ash.

Note that whether the information returned includes version information depends on whether a version is explicitly specified in the target module's manifest's RequiredModules entry.


Caveat: The above will not discover de facto dependencies that aren't also formally declared, based on the module's runtime behavior. Discovering such dependencies would be both nontrivial and brittle.

That said, if interactive discovery is an option, use the following as the first command from a pristine session started with -NoProfile, which may at least provide clues as to what dependent modules are de facto being imported at import time:

Import-Module -Verbose -Force $someModule

However, there could still be additional dependent modules that the module's commands import on demand, when they're first called.

Set-PSDebug with its -Trace parameter offers general tracing of code execution.

  • Related