How to Count the Number of Commands of an Installed PowerShell Module

Table of Contents

Count the number of commands of an installed module

In some cases, you want to know how many commands are available through a PowerShell module that you’ve installed in your computer.

1. Get the list of the installed PowerShell modules.

Get-InstalledModule
Version Name                       Repository
------- ----                       ----------
11.6.0  Az                         PSGallery
2.19.0  Az.Accounts                PSGallery
2.0.1   Az.Advisor                 PSGallery
6.0.3   Az.Aks                     PSGallery
1.1.4   Az.AnalysisServices        PSGallery
4.0.2   Az.ApiManagement           PSGallery
1.0.0   Az.App                     PSGallery
...

2. Get the list commands of a single PowerShell module.

Get-Command -Module 'MicrosoftTeams'
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Add-TeamChannelUser                                6.2.0      MicrosoftTeams
Cmdlet          Add-TeamUser                                       6.2.0      MicrosoftTeams
Cmdlet          Clear-TeamsEnvironmentConfig                       6.2.0      MicrosoftTeams
Cmdlet          Connect-MicrosoftTeams                             6.2.0      MicrosoftTeams
Cmdlet          Disconnect-MicrosoftTeams                          6.2.0      MicrosoftTeams
Cmdlet          Get-AllM365TeamsApps                               6.2.0      MicrosoftTeams
Cmdlet          Get-AssociatedTeam                                 6.2.0      MicrosoftTeams
...

3. Count the number of commands.

Get-Command -Module 'MicrosoftTeams' | measure | select Count
Count
-----
  556

4. Run the following command to count the number of commands of modules using a wildcard. For example, count the number of commands of the Azure PowerShell modules.

(Get-Command -Module "az*").Count
PS C:\> (Get-Command -Module "az*").Count
5796
PS C:\> (Get-Command -Module "*Microsoft.Graph.*").Count
8938

5. In case you want to build a report for all PowerShell modules. Use the below code snippet:

$array = @()
foreach ($module in Get-InstalledModule) {
        $a = (Get-Command -Module $($module.Name) | measure).Count
        $object = New-Object -TypeName PSObject -Property $([ordered]@{
            Name    = $module.Name
            Verison = $module.Version
            Count   = $a
        })
          $array += $object
    }
$array | Sort-Object -Property Count -Descending
Name                     Verison    Count
----                     -------    -----
Microsoft.Graph.Intune   6.1907.1.0  1569
MicrosoftTeams           6.2.0        556
AzureAD                  2.0.2.182    231
MSOnline                 1.1.183.81    96
ExchangeOnlineManagement 3.5.0         33
PowerShellGet            2.2.5         26
PSMDATP                  1.1.0         24
UniversalPrintManagement 1.13.0        16
PackageManagement        1.4.8.1       13

Count the number of commands in current session

For the current PowerShell session, the below command will list all imported modules:

Get-Module | select Name, Version
Name                                            Version
----                                            -------
Microsoft.Graph.Applications                    2.19.0
Microsoft.Graph.Authentication                  2.19.0
Microsoft.Graph.Bookings                        2.19.0
Microsoft.Graph.DeviceManagement.Actions        2.19.0
Microsoft.Graph.DeviceManagement.Administration 2.19.0
Microsoft.Graph.DeviceManagement.Enrollment     2.19.0
...

Count the total commands are available through the modules that have imported into the current Windows PowerShell session.

Get-Module | foreach {Get-Command -Module $_.Name} | measure | select Count
Count
-----
 7493

In case you want to get the details about each module. Use the Get-Module cmdlet and pipe the results to the Foreach loop. Inside the script block, use the Get-Command cmdlet to return the commands from each module. Send the results to the Measure-Object (measure is an alias) cmdlet, as shown here.

$array = @()
foreach ($module in Get-Module) {
        $a = (Get-Command -Module $($module.Name) | measure).Count
        $object = New-Object -TypeName PSObject -Property $([ordered]@{
            Name    = $module.Name
            Verison = $module.Version
            Count   = $a
        })
          $array += $object
    }
$array | Sort-Object -Property Count -Descending
Name                                            Verison Count
----                                            ------- -----
Microsoft.Graph.Devices.CorporateManagement     2.19.0   1133
Microsoft.Graph.Files                           2.19.0   1076
Microsoft.Graph.Sites                           2.19.0   1018
Microsoft.Graph.Teams                           2.19.0    654
MicrosoftTeams                                  6.2.0     556
Microsoft.Graph.Identity.DirectoryManagement    2.19.0    354
Microsoft.Graph.Groups                          2.19.0    350
...

Leave a Comment

Required fields are marked *