thisextendsthat
thisextendsthat

Reputation: 1335

Powershell DistinctBy Condition

How can I filter a list in PowerShell to be distinct by a condition?

I need to select file paths which differ as absolute paths BUT filter out duplicates by file name (regardless of where they are).

So for these file paths:

D:\MyFolder\MyProject.dll

D:\MyFolder\MySubfolder\MyProject.dll

Get-Unique won't work because they are unique. How can I select the absolute path but make it distinct by file name only?

Current script looks like this:

get-childitem D:\MyFolder -recurse -filter "*.dll" | Select -expand FullName

How can I just it to return (either) one of the above on the condition that FileName = MyProject.dll

Upvotes: 0

Views: 122

Answers (1)

Jacob Colvin
Jacob Colvin

Reputation: 2835

You could sort -unique on the property before expanding:

Get-ChildItem -Path 'D:\MyFolder' -Recurse -Filter '*.dll' | 
  Sort-Object -Unique -Property Name | 
  Select-Object -ExpandProperty FullName

Upvotes: 2

Related Questions