Get Commands in a Nested Module
- Details
- Written by June Blender
- Last Updated: 09 October 2019
- Created: 19 August 2015
- Hits: 13071
Many Windows PowerShell modules include nested modules. In fact, some of your favorite cmdlets might be in a nested module, but you wouldn’t typically know it. Nested modules are reusable, so you can repackage and use nested modules independently and in other modules, unless, of course, they require something in the parent module. For example, you can use a nested module in a custom session configuration when the user doesn’t need and shouldn’t have the entire module.
The ModuleInfo and ModuleInfoGrouping objects that the Get-Module cmdlet returns have a NestedModules property. So, to find modules with nested modules, use this command:
Get-Module –ListAvailable | where NestedModules |
To find the nested modules in a particular module, use a command with this format:
(Get-Module –ListAvailable $ModuleName).NestedModules |
For example, the NetAdapter module has 19 nested modules. (CIM modules typically have lots of nested modules.)
PS C:\> (Get-Module -ListAvailable NetAdapter).NestedModules ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Cim 1.0 MSFT_NetAdapter.cmdletDefinition {Get-NetAdapter, Enable-N Cim 1.0 MSFT_NetAdapterAdvancedProperty.... {Get-NetAdapterAdvancedPr Cim 1.0 MSFT_NetAdapterBinding.cmdletDef... {Get-NetAdapterBinding, S Cim 1.0 MSFT_NetAdapterChecksumOffload {Get-NetAdapterChecksumOf Cim 1.0 MSFT_NetAdapterEncapsulatedPacke... {Get-NetAdapterEncapsulat Cim 1.0 MSFT_NetAdapterHardwareInfo.cmdl... Get-NetAdapterHardwareInf Cim 1.0 MSFT_NetAdapterIPsecOffload {Get-NetAdapterIPsecOfflo Cim 1.0 MSFT_NetAdapterLso {Get-NetAdapterLso, Set-N Cim 1.0 MSFT_NetAdapterPowerManagement.c... {Get-NetAdapterPowerManag Cim 1.0 MSFT_NetAdapterQos {Get-NetAdapterQos, Set-N Cim 1.0 MSFT_NetAdapterRdma {Get-NetAdapterRdma, Set- Cim 1.0 MSFT_NetAdapterRsc {Get-NetAdapterRsc, Set-N Cim 1.0 MSFT_NetAdapterRss.cmdletDefinition {Get-NetAdapterRss, Set-N Cim 1.0 MSFT_NetAdapterSriov {Get-NetAdapterSriov, Set Cim 1.0 MSFT_NetAdapterSriovVf.cmdletDef... Get-NetAdapterSriovVf Cim 1.0 MSFT_NetAdapterStatistics.cmdlet... Get-NetAdapterStatistics Cim 1.0 MSFT_NetAdapterVmq.cmdletDefinition {Get-NetAdapterVmq, Set-N Cim 1.0 MSFT_NetAdapterVmqQueue.cmdletDe... Get-NetAdapterVmqQueue Cim 1.0 MSFT_NetAdapterVPort.cmdletDefin... Get-NetAdapterVPort
Sadly, you can’t use Module parameter of Get-Command to get the commands in a nested module.
PS C:\> Get-Command -Module MSFT_NetAdapterQos PS C:\>
But, it’s easy to get them. The NestedModules property contains a ReadOnlyCollection of PSModuleInfo objects. Each one has all of the Exported properties of any module, including ExportedCmdlets, ExportedFunctions, and ExportedCommands, which includes all exported command types.
(Get-Module NetAdapter -ListAvailable).NestedModules | Format-Table –Property Name, ExportedCommands –Wrap |
The ExportedCommands are in dictionaries (like a hash table), so, to get the commands in a given nested module, use a command like this one:
((Get-Module $ModuleName –ListAvailable).NestedModules | where Name –eq $NestedModuleName).ExportedCommands.Keys |
For example:
PS C:\> ((Get-Module -ListAvailable NetAdapter).NestedModules | where Name -eq MSFT_NetAdapterQos).ExportedCommands.Keys Get-NetAdapterQos Set-NetAdapterQos Enable-NetAdapterQos Disable-NetAdapterQos
You can also find out which nested module exports a particular command:
PS C:\> (Get-Module –ListAvailable NetAdapter).NestedModules | where {$_.ExportedCommands.Keys -eq "Enable-NetAdapterQos"} ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Cim 1.0 MSFT_NetAdapterQos {Get-NetAdapterQos, Set-NetAdapterQos, Enable-NetAdapterQos
To get the source file for a nested module, look in the value of its Path property.
PS C:\> (Get-Module NetAdapter -ListAvailable).NestedModules | Format-List Name, Path Name : MSFT_NetAdapter.cmdletDefinition Path : C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\NetAdapter\MSFT_NetAdapter.cmdletDef Name : MSFT_NetAdapterAdvancedProperty.cmdletDefinition Path : C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\NetAdapter\MSFT_NetAdapterAdvancedPr Name : MSFT_NetAdapterQos Path : C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\NetAdapter\MSFT_NetAdapterQos.cdxml ...
And, use the Path value to import the module.
PS C:\> Import-Module $pshome\Modules\NetAdapter\MSFT_NetAdapterQos.cdxml PS C:\> Get-Module ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Computer, Add-Conte Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, C Cim 1.0 MSFT_NetAdapterQos {Disable-NetAdapterQos,
Be sure to test, because many nested modules require other modules.
June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at This email address is being protected from spambots. You need JavaScript enabled to view it. or follow her on Twitter at @juneb_get_help.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.