DISTINCT Selection-String output in directory / text search

I am wondering how to create a separate file list based on this example.

** This example creates a list of all .ps1 and .psm1 files containing 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 a duplicate of the file output (same file, but different line numbers).

How can I save my file output?

Current unwanted result:

  • C: \ Workspace \ mydirectorytosearch1 \ Anonymization-psake.ps1: 4 :. "$ ($ folders.example.test) \ Anonymization \ Example.vars.ps1" C: \ Workspace \ mydirectorytosearch1 \ Anonymization-psake.ps1: 5 :. "$ ($ Folders.missles) \ extract \ built-in utilities.ps1"

Desired Result:

  • C: \ Workspace \ mydirectorytosearch1 \ Anonymization-psake.ps1

Help me customize my script

+4
source share
1 answer

You can eliminate duplicates with 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 
+6
source

Source: https://habr.com/ru/post/1443267/


All Articles