Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Sort-Object -Unique

I have an array which contains a variety of TaskNumbers and I am trying to obtain the members which are unique and those which are non-unique(as I have to take different actions for each)

Now, if I use Sort-Object -Property TaskNumber -Unique, it returns 1 non-unique value for each set of non-unique values it finds(See TK-15386 below). I need to actually get the values that are unique, as opposed to one result being included that is in reality not unique at all. Even if I could flag this solo returned non-unique value in some way I could account for it later on. Anyone got any ideas? Am using PS v4 but could upgrade if there is a fix somehow in v5.

$Thisweekarray | Select-Object -Property TaskNumber | Sort-Object -Property TaskNumber 

TK-02213                                                                                                                                                                             
TK-02242                                                                                                                                                                             
TK-15386                                                                                                                                                                             
TK-15386                                                                                                                                                                             
TK-15386                                                                                                                                                                             
TK-15386                                                                                                                                                                             
TK-15387                                                                                                                                                                             

$Thisweekarray | Select-Object -Property TaskNumber | Sort-Object -Property TaskNumber -Unique                                                                                                                                                                          

TK-02213                                                                                                                                                                             
TK-02242                                                                                                                                                                             
TK-15386                                                                                                                                                                             
TK-15387 
like image 646
Meatweasel Avatar asked Jun 06 '26 15:06

Meatweasel


1 Answers

You may use the Group-Object cmdlet to group your list based on the TaskNumber. This will allow you to filter your objects based on the occurrence of a property.

The following snippet will return every TaskNumber that only occur once:

$Thisweekarray | 
    Group-Object TaskNumber | 
    Where-Object Count -eq 1 | 
    Select-Object -Expand Group 

Just use Where-Object Count -gt 1 to obtain the TaskNumber with multiple occurrence.

like image 86
Martin Brandl Avatar answered Jun 08 '26 10:06

Martin Brandl