Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select distinct items from a column in powershell

Have you tried something like this?

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

Even shorter:

get-command | select-object  moduleName -unique

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}

Another option:

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