D3vtr0n
D3vtr0n

Reputation: 2909

DISTINCT Select-String output on directory/text search

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:

The desired output:

Help me tweak my script??

Upvotes: 6

Views: 5309

Answers (1)

Shay Levy
Shay Levy

Reputation: 126762

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

Upvotes: 8

Related Questions