59

If I issue following command in PowerShell, I get a lot of rows back.

PS C:\Users\benh> get-command

CommandType     Name                               ModuleName                         Definition
-----------     ----                               ----------                         ----------
Cmdlet          Get-Variable                       Microsoft.PowerShell.Utility       Get-Variable...
Cmdlet          Get-WebAppDomain                   WebAdministration                  Get-WebAppDomain...
Cmdlet          Get-WebApplication                 WebAdministration                  Get-WebApplication...
Cmdlet          Get-WebAppPoolState                WebAdministration                  Get-WebAppPoolState...
...
Cmdlet          Get-WinEvent                       Microsoft.PowerShell.Diagnostics   Get-WinEvent...
Cmdlet          Get-WmiObject                      Microsoft.PowerShell.Management    Get-WmiObject...
Cmdlet          Get-WSManCredSSP                   Microsoft.WSMan.Management         Get-WSManCredSSP...
Cmdlet          Get-WSManInstance                  Microsoft.WSMan.Management         Get-WSManInstance...
Cmdlet          Group-Object                       Microsoft.PowerShell.Utility       Group-Object...
Cmdlet          Import-Alias                       Microsoft.PowerShell.Utility       Import-Alias...
Cmdlet          Import-Clixml                      Microsoft.PowerShell.Utility       Import-Clixml...
Cmdlet          Import-Counter                     Microsoft.PowerShell.Diagnostics   Import-Counter...
Cmdlet          Import-Csv                         Microsoft.PowerShell.Utility       Import-Csv...
Cmdlet          Import-LocalizedData               Microsoft.PowerShell.Utility       Import-LocalizedData...
Cmdlet          Import-Module                      Microsoft.PowerShell.Core          ...

What I want to do is get all the distinct ModuleNames returned by Get-Command. How can I do this with PowerShell?

In pseudo-C#:

PowerShell.Exec("Get-Command").Select(a=> a.ModuleName).Distinct();
starball
  • 20,030
  • 7
  • 43
  • 238
Ben H
  • 3,136
  • 3
  • 25
  • 34

4 Answers4

97

Have you tried something like this?

get-command | select ModuleName | sort-object -Property ModuleName -Unique
srgerg
  • 18,719
  • 4
  • 57
  • 39
  • 1
    Plain old *select* was exactly what I was looking for, thanks! – Ben H Dec 10 '11 at 18:05
  • +1 as this solution is case insensitive, Ex, getting AD computer names and DNS records and trying to show only unique names when the capitalization might be different. – Chris Magnuson Mar 02 '17 at 13:27
  • I'm new to PS. Can you explain why the sort-object is needed? I was attempting to do "| select -unique" –  Nov 08 '18 at 19:45
68

Even shorter:

get-command | select-object  moduleName -unique
Mike Shepard
  • 17,466
  • 6
  • 51
  • 69
  • 3
    Fyi, this is case sensitive, for case insensitive use `Sort-Object -unique` like in [srgerg's answer](http://stackoverflow.com/a/8439487/101679). [Reference](https://blogs.technet.microsoft.com/heyscriptingguy/2012/01/15/use-powershell-to-choose-unique-objects-from-a-sorted-list/) – Chris Magnuson Mar 02 '17 at 13:31
  • Good to know that -unique in Select-object is case-sensitive. Are module names case sensitive, though? Is it possible to have 2 modules loaded where the only difference is in case? – Mike Shepard Mar 03 '17 at 17:11
  • I don't think so, this would be more relevant to the title of the question vs the specific use case of the asker. – Chris Magnuson Mar 04 '17 at 02:39
7

Below 2 commands will bring out same result, but the first one will be sorted and a bit expensive in execution time.

The execution time will be taken more into consideration when you have large number of items, For example if you are importing csv file of 30,000 rows. Then second option will be faster, and once you get unique values sort them if you need because here sorting will be done on a much lesser number of items hence better performance.

1.

get-command | select ModuleName | sort-object -Property ModuleName -Unique

# This will give you the execution time
Measure-Command {get-command | select ModuleName | sort-object -Property ModuleName -Unique}

2.

get-command | select ModuleName -Unique

# This will give you the execution time
Measure-Command {get-command | select ModuleName -Unique}
Asad Refai
  • 255
  • 4
  • 16
5

Another option:

Get-Command | Group-Object ModuleName -NoElement | Select-Object Name
Shay Levy
  • 121,444
  • 32
  • 184
  • 206