One of my clients was working on a CSV file, and they were required to find unique values from that CSV file. I suggested using PowerShell and told them a few methods. In this tutorial, I will explain to you four simple and useful methods to get unique values from CSV using PowerShell.
Get Unique Values from CSV Using PowerShell
PowerShell provides various cmdlets to work with CSV files. In combination with a few other cmdlets, you can be able to get unique values from a CSV file in PowerShell.
Here are the methods with examples.
For all the methods, I am using the below CSV file, which has the below content. You can even download the CSV file and use it.
Name,Age
Alice,30
Bob,25
Alice,30
Charlie,35
Bob,25Method 1: Using Select-Object -Unique
One way to get unique values from a CSV file in PowerShell is by using the Select-Object cmdlet with the -Unique parameter.
This method is also very simple to use, and here is the complete PowerShell script to extract unique rows based on the Name column.
# Import the CSV file
$data = Import-Csv -Path "C:\MyFolder\data.csv"
# Select unique rows based on the 'Name' column
$uniqueData = $data | Select-Object -Property Name -Unique
# Output the unique rows
$uniqueDataThis script will output:
Name
----
Alice
Bob
CharlieI executed the above script using VS code and you can see the output in the screenshot below:

Method 2: Using Group-Object
Another efficient way to get unique values from a CSV file in PowerShell is by using the Group-Object cmdlet.
Here also, we will use the same data.csv file. We can group the rows by the Name column and then select the first instance of each group:
Below is the complete PowerShell script.
# Import the CSV file
$data = Import-Csv -Path "C:\MyFolder\data.csv"
# Group by 'Name' and select the first instance of each group
$uniqueData = $data | Group-Object -Property Name | ForEach-Object { $_.Group[0] }
# Output the unique rows
$uniqueDataThis script will output:
Name Age
---- ---
Alice 30
Bob 25
Charlie 35After executing the above PowerShell script, I got the required output, and you can see the output in the screenshot below:

Method 3: Using Sort-Object and Get-Unique
The PowerShell Sort-Object cmdlet can be combined with Get-Unique to sort the data first and then remove duplicates. This method requires the input data to be sorted.
Using the same data.csv file, you can sort by the Name column and then get unique values; here is the complete script.
# Import the CSV file
$data = Import-Csv -Path "C:\MyFolder\data.csv"
# Sort by 'Name' and then get unique values
$uniqueData = $data | Sort-Object -Property Name | Get-Unique -AsString
# Output the unique rows
$uniqueDataThis script will output:
Name Age
---- ---
Alice 30
Bob 25
Charlie 35Method 4: Using HashSet
Here is the final method to get unique values from an Excel file in PowerShell using HashSet.
Using the same data.csv file, you can use a HashSet to store unique values like below:
# Import the CSV file
$data = Import-Csv -Path "C:\MyFolder\data.csv"
# Create a HashSet to store unique names
$hashSet = New-Object System.Collections.Generic.HashSet[string]
# Iterate over each row and add unique names to the HashSet
foreach ($row in $data) {
[void]$hashSet.Add($row.Name)
}
# Output the unique names
$hashSetThis script will output:
Alice
Bob
CharlieConclusion
In this PowerShell tutorial, I have explained how to get unique values from a CSV file using PowerShell. You can use various methods like the Select-Object -Unique, Group-Object, Sort-Object with Get-Unique, or a HashSet, etc. I hope this helps you.
You may like the following tutorials:
- How to Get Unique Lines from a File Using PowerShell?
- Count Duplicate Lines in a Text File Using PowerShell
- Count Words in a File Using PowerShell
- How to Append Text to a File in PowerShell?
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.