Video Tutorial
5 ways to select Unique items in PowerShell
Following are the approaches that I could come up with, there may be few more, feel free to point them out and I’ll update the article
-
- Using
Select-Object
CmdletThis is probably already your favorite way to find unique items in PowerShell
- Using
Get-Unique
CmdletThe catch with this cmdlet is – it only works with sorted objects by comparing adjacent items.
- Using
[HashSet<T>]
HashSets are data structures that only hold unique items, you can typecast your array in our example to a
[HashSet]
I was not aware that, .Net provides a class for HashSets which I learned a few weeks back and to my wonder, they seem to be better in performance.But in case you are worried, typecasting to HashSet will change the data type and you can’t perform methods available like in the data type [Array], then please be aware that you can typecast them back toSystem.Array
, like in the below image. - Using LINQ
Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language, and the good news is that it can be used directly in PowerShell
- Using
Sort-Object
cmdletTo my surprise, even the
Sort-Object
cmdlet has a-Unique
parameter switch that can select unique objects passed through the pipeline.
- Using
NOTE: The performance varies in all 4 approaches, in the following screenshot you can clearly see the difference, the use of pipeline probably slows down the cmdlets: Select-Object
and Get-Unique
.
If you like this article read more similar articles under ‘N – Ways to’ category
- 4 ways to send output to NULL in PowerShell
- 3 ways to store and display PowerShell Variable simultaneously
Prateek Singh
Related posts
7 Comments
Leave a ReplyCancel reply
Categories
Author of Books
Awards
Open Sourced Projects
Author at
Blog Roll
Mike F RobbinsDamien Van Robaeys
Stéphane van Gulick
Kevin Marquette
Adam Bertram
Stephanos Constantinou
Francois-Xavier Cat
Ravikanth Chaganti
Roman Levchenko
Blog Stats
- 1,132,948 People Reached
Great article!
Also, Sort-Object has a -Unique parameter as well! 😉
[…] Unique Elemente erhalten – auf 4 Arten: Ridicurious […]
[…] 4 ways to select Unique items in PowerShell […]
[…] 4 ways to select Unique items in PowerShell […]
[…] 4 ways to select Unique items in PowerShell […]
Another method is to use Group-Object
“$set | select -unique” with the above sample gave an average result of 0.0732245000000091.
“Group $set” gave an average of 0.0612960800000009
This starts adding up when comparing multiple fields.
For a sample of 19456 records and selecting two unique fields from the set, “$set | Group A,B” (while adding the output to a result array) took only 34367.7835ms.
“$set | select -unique A,B” took 165405.4153ms – nearly 5x longer.
As a note for anyone who wants to use the Group method for multiple fields, an array of unique group objects is created for each result. Inside each object, any duplicate values are contained in the Group property. This means that capturing the first value from each Group property is unique for the combination of fields.
I have a set where the A field is a user name and the B field is an IP address. I group my $set into a new array by IP, then UserName – “$grp = $set | Group B,A”
The first result appears as below:
$grp[0] | ConvertTo-Json
{
“Values”: [
“10.180.9.40”
],
“Count”: 4,
“Group”: [
{
“A”: “MYDOMAIN\\MyApp”,
“B”: “10.180.9.40”
},
{
“A”: “MYDOMAIN\\MyApp”,
“B”: “10.180.9.40”
}
],
“Name”: “10.180.9.40”
}
The next item in the result array has the same account logging on from a different IP – there were 4 logons in total from the account
$grp[1] | ConvertTo-Json
{
“Values”: [
“10.180.9.41”
],
“Count”: 4,
“Group”: [
{
“User”: “MYDOMAIN\\MyApp”,
“IP”: “10.180.9.41”
},
{
“User”: “MYDOMAIN\\MyApp”,
“IP”: “10.180.9.41”
}
],
“Name”: “10.180.9.41”
}
To select Item 0 from each of the Group properties, we create another array and dump the unique results into that
$results = @()
$grp | foreach { $results += $_.group[0] }
The final result looks like this and is still quicker than the -select Unique method:
$results |ft -AutoSize
IP User
— —-
10.180.9.40 MYDOMAIN\\MyApp
10.180.9.41 MYDOMAIN\\MyApp
considering powershell behaves different when command line and in file, could you put up a | select – Unque etc example that works in file.
My Sort -unique gives 2 lines of output – but when I run it as a ps1, it shows yes 0 length, then 1 length. goes to next lne in ps1 file now shows 15 length