Remove 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"

1

 

 

 

 

 

 

 

 

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

2

 

 

 

The second method:

Get-Process | Where-Object name -eq "svchost" | Get-Unique

3

 

 

 

 

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}

4

 

 

 

 

 

Measure-Command {Get-Process | Where-Object name -eq "svchost" | Get-Unique}

5

 

 

 

 

 

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!