select distinct items from a column in powershell

Solution 1:

Have you tried something like this?

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

Solution 2:

Even shorter:

get-command | select-object  moduleName -unique

Solution 3:

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}

Solution 4:

Another option:

Get-Command | Group-Object ModuleName -NoElement | Select-Object Name