select distinct items from a column in powershell

Ben H picture Ben H · Dec 9, 2011 · Viewed 70.5k times · Source

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();

Thanks in advance!

Answer

srgerg picture srgerg · Dec 9, 2011

Have you tried something like this?

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