PowerShell Select-Object -Unique [With Examples]

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 -Unique

This 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> -Unique

The -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
$uniqueEmployees

In 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:

PowerShell Select-Object -Unique

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

This 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
$uniqueAddresses

In this scenario, Select-Object -Unique ensures that each city-state combination is unique.

Here is the exact output in the screenshot below:

PowerShell Select-Object -Unique Examples

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
$sortedUniqueEmployees

This 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:

Select-Object -Unique in PowerShell

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.