DISTINCT Select-String output on directory/text search

powershell, powershell-2.0, powershell-3.0, powershell-ise, scripting

Solution

You can eliminate duplicates wit Select-String and the Unique parameter:

$text='folders'
Get-ChildItem C:\Workspace\mydirectorytosearch1\,C:\Workspace\mydirectorytosearch2\ -Recurse -Filter '*.ps*1' |
Select-String -Pattern $text | Where-Object {$_ -NotLike '*.invoke(*'} | 
Select-Object Path -Unique

Problem

I am curious how to produce a distinct file list based on this example. ** This example produces a list of all .ps1 and .psm1 files that contain the text "folders", but without the text ".invoke" on the same line. ``` $text='folders' dir C:\Workspace\mydirectorytosearch1\ -recurse -filter '*.ps*1' | Get-ChildItem | select-string -pattern $text | where {$_ -NotLike '*.invoke(*'} dir C:\Workspace\mydirectorytosearch2\ -recurse -filter '*.ps*1' | Get-ChildItem | select-string -pattern $text | where {$_ -NotLike '*.invoke(*'} ``` This is cool and works well but I get duplicate file output (same file but different line numbers). How can I keep my file output distinct? The current undesirable output: - C:\Workspace\mydirectorytosearch1\anonymize-psake.ps1:4:. "$($folders.example.test)\anonymize\Example.vars.ps1" - C:\Workspace\mydirectorytosearch1\anonymize-psake.ps1:5:. "$($folders.missles)\extract\build-utilities.ps1" The desired output: - C:\Workspace\mydirectorytosearch1\anonymize-psake.ps1 Help me tweak my script??

Original source