Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Get-Unique and select-object -unique

Using PowerShell, I import csv-data from file via "import-csv" into the object $csvList. This csv-data has a column called Benutzer. When doing something like this:

$csvList | %{$_.Benutzer} | select-object -unique

there is nothing special: as expected, the unique entries in Benutzer are returned, which are around 10 items. However,

$csvList | %{$_.Benutzer} | get-unique -asstring

or

$csvList | Group($_.Benutzer)

seem to treat each entry as unqiue, i.e. the entire array of 260 Benutzer is returned in case of get-unique and 260 groups are created in case of the group statement. I am using PowerShell 4.0.

Any idea what is going on here is appreciated...

like image 998
StefG Avatar asked Oct 13 '14 06:10

StefG


1 Answers

From Get-Unique help:

The Get-Unique cmdlet compares each item in a sorted list...

Select-Object -Unique does not require objects to be pre-sorted.

Example:

PS> 9,8,9,8 | Get-Unique -AsString
9
8
9
8

Example:

PS> 9,8,9,8 | Sort-Object -Unique
8
9

As for Group-Object, the syntax of the command should be different, replace ($_.Benutzer) with the property name Benutzer:

$csvList | Group-Object Benutzer
like image 180
Roman Kuzmin Avatar answered Nov 16 '22 02:11

Roman Kuzmin