Remove duplicates
Posted in Duplicates
Often i need to remove duplicates in objects before pipe them to another function, or before print the result.
So, let’s take a cmdlet which return many lines like Get-Process
Get-Process | Where-Object name -eq "svchost"
Now, let’s remove duplicates from the output… Two methods exist for this task…
The first one:
Get-Process | Where-Object name -eq "svchost" | Select-Object -Unique
The second method:
Get-Process | Where-Object name -eq "svchost" | Get-Unique
Ok, which one should i use ? Kinda difficult to answer that, two of them return exactly the same output… The main difference between them is performance related, look at this !
Measure-Command {Get-Process | Where-Object name -eq "svchost" | Select-Object -Unique}
Measure-Command {Get-Process | Where-Object name -eq "svchost" | Get-Unique}
Ok, we talk only about Milliseconds, but clearly on bigger objects collection it could be far better.
Hope you’ll find how to use this.
See ya!