In this tutorial, I will explain how to work with PowerShell Select-Object -Unique especially how to use Select-Object -Unique to filter out unique objects in your data sets.
To filter unique objects in PowerShell using the Select-Object -Unique cmdlet, you can streamline your data by specifying the property you want to ensure is unique. For example, to get a list of unique employee names from a dataset, you can use:
$employees | Select-Object -Property Name -UniqueThis command will filter the dataset and return only unique employee names, effectively removing any duplicates.
PowerShell Select-Object -Unique
The Select-Object cmdlet in PowerShell that allows you to select specific properties of an object or set of objects. When combined with the -Unique parameter, it ensures that only unique objects are selected, effectively removing duplicates from your data.
Syntax
Here’s the basic syntax for using Select-Object with the -Unique parameter:
Select-Object -Property <PropertyName> -UniqueThe -Unique parameter in Select-Object is designed to filter out duplicate objects based on the properties you specify.
Now, let me show you some examples.
PowerShell Select-Object -Unique Examples
Here are some practical examples to see how this works.
Example 1: Filter Unique Names
Imagine you have a list of employees working in various departments across the USA. You want to get a list of unique employee names.
Here is the complete PowerShell script.
$employees = @(
[PSCustomObject]@{Name="John Doe"; Department="IT"},
[PSCustomObject]@{Name="Jane Smith"; Department="HR"},
[PSCustomObject]@{Name="John Doe"; Department="Finance"},
[PSCustomObject]@{Name="Emily Davis"; Department="IT"}
)
$uniqueEmployees = $employees | Select-Object -Property Name -Unique
$uniqueEmployeesIn this example, Select-Object -Unique filters out the duplicate entry for “John Doe,” resulting in a unique list of employee names.
Here is the exact output in the screenshot below:

Check out PowerShell Select-Object Without Header
Example 2: Unique Entries in a CSV File
Suppose you have a CSV file containing customer data from different states in the USA, and you want to filter out unique customer entries based on their email addresses.
$customers = Import-Csv -Path "C:\MyFolder\customers.csv"
$uniqueCustomers = $customers | Select-Object -Property Email -Unique
$uniqueCustomers | Export-Csv -Path "C:\MyFolder\unique_customers.csv" -NoTypeInformationThis script imports the customer data, filters out duplicates based on the Email property, and exports the unique entries to a new CSV file.
Example 3: Combine Multiple Properties
Sometimes, you might need to ensure uniqueness based on a combination of properties. For example, you might want unique combinations of city and state from a list of addresses.
$addresses = @(
[PSCustomObject]@{City="New York"; State="NY"},
[PSCustomObject]@{City="Los Angeles"; State="CA"},
[PSCustomObject]@{City="New York"; State="NY"},
[PSCustomObject]@{City="Chicago"; State="IL"}
)
$uniqueAddresses = $addresses | Select-Object -Property City, State -Unique
$uniqueAddressesIn this scenario, Select-Object -Unique ensures that each city-state combination is unique.
Here is the exact output in the screenshot below:

Check out PowerShell Write-Output
Example 4: Using Select-Object with Other Cmdlets
You can combine Select-Object -Unique with other cmdlets for more complex data manipulation. For instance, you might want to sort a list before selecting unique entries.
$employees = @(
[PSCustomObject]@{Name="John Doe"; Department="IT"},
[PSCustomObject]@{Name="Jane Smith"; Department="HR"},
[PSCustomObject]@{Name="John Doe"; Department="Finance"},
[PSCustomObject]@{Name="Emily Davis"; Department="IT"}
)
$sortedUniqueEmployees = $employees | Sort-Object -Property Name | Select-Object -Property Name -Unique
$sortedUniqueEmployeesThis script sorts the employees by name before filtering out duplicates, ensuring a sorted list of unique names.
Here is the exact output in the screenshot below:

Conclusion
In this tutorial, I used various real examples to explain how to work with Select-Object -Unique in PowerShell.
You may also like the following tutorials:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.