Martin Muldoon
Martin Muldoon

Reputation: 3428

How to group and select unique values in PowerShell?

I've asked this question on the SharePoint StackExchange w/ no luck

I have a CSV file with one column, SharePoint Site URLs, many of which are duplicates

Column Title
Site C URL
Site A URL
Site B URL
Site C URL
Site C URL
Site A URL

How can I retrieve the rows so the I only have unique sites:

Site A URL 
Site B URL 
Site C URL 

I've tried:

$CSVLocation = 'C:\data\data.csv'
$siteURLs = Import-CSV -Path $CSVLocation 
$siteURLs | Group-Object -Property 'Column Title' | Select-Object -Unique

Upvotes: 1

Views: 1543

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25001

You can approach this problem several ways. One simple way is to use Sort-Object provided you only have the one column.

$siteURLs | Sort-Object 'Column Title' -Unique

You can also use Group-Object. Group-Object by default outputs three properties: Count, Name, Group. Since you are grouping by Column Title, those values will appear under Name. So if you only want to see that data, you need to select Name.

$siteURLs | Group-Object -Property 'Column Title' | Select-Object Name

Upvotes: 3

Related Questions