PowerShell One-Liner to List All Modules and Return How Many Cmdlets Each One Contains

A big misconception when it comes to PowerShell one-liners is that they're one line of code as the name implies. That assumption is incorrect though because a one-liner can be written in one physical line of code such as in this example:

1Get-Module -ListAvailable | Import-Module -PassThru | ForEach-Object {Get-Command -Module $_} | Group-Object -Property ModuleName -NoElement | Sort-Object -Property Count -Descending

Or it can be written in multiple physical lines of code such as in the next example. How can that be? That's because a one-liner is one continuous pipeline.

1Get-Module -ListAvailable |
2Import-Module -PassThru |
3ForEach-Object {Get-Command -Module $_} |
4Group-Object -Property ModuleName -NoElement |
5Sort-Object -Property Count -Descending

One thing I also want you to notice is the NoElement parameter that was used with the Group-Object cmdlet. Using that parameter keeps you from having to do additional unnecessary work such as piping the results to the Select-Object cmdlet or to one of the Format cmdlets to remove that property from your results.

021314-1.png

Although the next command accomplishes the same task and it's on one physical line, it's not a one-liner since it contains a semicolon which breaks the pipeline (it's not one continuous pipeline):

1$Modules = Get-Module -ListAvailable | Import-Module; Get-Command -Module $Modules | Group-Object -Property ModuleName -NoElement | Sort-Object -Property Count -Descending

If I were only typing this one-liner interactively and didn't plan to save it as a script, the use of aliases and positional parameters would be acceptable, however in my opinion you should avoid the use of aliases and positional parameters when publishing content such as this blog article to avoid confusion.

Note: The commands shown in this blog article return a list of all modules and the number of cmdlets that each of them contain that are installed on the machine they're being run on. If a module is located somewhere other than a path found in the $env:PSModulePath environment variable, it will not be returned by these commands unless it's explicitly imported. One module that I commonly work with that falls into this category is the Dell EqualLogic SAN PowerShell module.

µ