Filter Unique Objects in PowerShell with Where-Object

Recently, I got a requirement to filter out unique objects from a data collection in PowerShel. PowerShell provides the Where-Object cmdlet to filter unique or distinct objects from a collection. In this PowerShell tutorial, I will explain how to filter unique objects in PowerShell with the Where-Object cmdlet.

To filter unique objects in PowerShell, you can use the Select-Object cmdlet with the -Unique parameter. For example, Get-Process | Select-Object -Property ProcessName -Unique will return a list of processes with distinct names. If you need to filter based on multiple properties, you can combine Where-Object with a custom script block or use Group-Object to group by properties and then select groups with a count of one.

PowerShell where-object distinct

The Where-Object cmdlet in PowerShell is used to filter objects based on their property values. It allows you to specify a script block that determines whether or not an object should be included in the output. The cmdlet evaluates each object in the pipeline and passes through those objects that match the specified condition.

However, Where-Object on its own does not directly provide a way to filter for unique objects. It would be best if you often used it in conjunction with other cmdlets to achieve this. The goal is to retrieve objects where a particular property or a combination of properties is distinct.

Use Group-Object with Where-Object to Get Unique Objects

One way to filter unique objects in PowerShell is to use the Group-Object cmdlet. This cmdlet groups objects that have the same value for specified properties. Here’s a basic example:

$processes = Get-Process
$uniqueProcesses = $processes | Group-Object -Property ProcessName | Where-Object { $_.Count -eq 1 } | Select-Object -ExpandProperty Group

In this example, we get a list of processes and then group them by ProcessName. We then use Where-Object to filter these groups to only those that have a count of 1, meaning they are unique. Finally, we expand the Group property to get the original objects back.

You can see the output in the screenshot below:

powershell where-object distinct

Combine Select-Object with Where-Object to Get Distinct Objects

You can also use Select-Object with Where-Object to get distinct objects in PowerShell. Here, you can use Select-Object with its -Unique parameter.

$uniqueProcesses = Get-Process | Select-Object -Property ProcessName -Unique

This will return process objects with unique ProcessName properties directly. However, if you need to filter based on a combination of properties, you might still need to employ Where-Object to fine-tune your results.

Advanced Filtering with Custom Script Blocks

For more complex scenarios where you might want to filter based on multiple properties or custom logic, you can use a custom script block with Where-Object. For example:

$uniqueItems = Get-Process | Where-Object {
    $currentProcess = $_
    -not ($processes | any { $_.Id -ne $currentProcess.Id -and $_.ProcessName -eq $currentProcess.ProcessName })
}

In this script block, we are checking for processes with a unique ProcessName that do not have any other process with the same name but a different Id.

Sort and Use Get-Unique to Get Unique Objects in PowerShell

Another method you can use to find unique objects in PowerShell involves sorting the objects first and then using Get-Unique cmdlet:

$sortedProcesses = Get-Process | Sort-Object -Property ProcessName
$uniqueProcesses = $sortedProcesses | Get-Unique -AsString

Here, Get-Unique is used after sorting the processes by ProcessName. The -AsString parameter is used to compare the property values as strings, which is necessary for non-primitive objects.

Note: When working with large sets of data, performance can become an issue. It’s important to choose the most efficient approach for your specific scenario. Using Select-Object -Unique is generally faster than grouping objects, especially when dealing with simple cases of uniqueness based on a single property.

Conclusion

Filtering for unique objects in PowerShell requires a combination of cmdlets like Group-Object, Select-Object, or a custom Where-Object filter. Here, I have explained different examples for filtering unique objects in your PowerShell scripts.

In this PowerShell tutorial, I have explained how to filter unique objects in PowerShell using Where-Object.

  • PowerShell where-object distinct
  • PowerShell where-object unique

You may also like: