5

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??

Liam
  • 27,717
  • 28
  • 128
  • 190
D3vtr0n
  • 2,774
  • 3
  • 33
  • 53

1 Answers1

8

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
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Sweet! Thanks! I had a problem with the output directories being truncated in the console window so I wrote an extra command to write the output to a text file with a fixed width instead, like so: | Out-File C:\Workspace\output.txt -width 200 – D3vtr0n Oct 31 '12 at 21:07
  • 3
    You can control the console window size if it helps with the truncating issue. $host.UI.RawUI.BufferSize = new-object System.Management.Automation.Host.Size(512,512); – Webplanet TFS Consulting Nov 02 '12 at 11:07