13

I am searching a directory for pattern, switches are -SimpleMatch -List. But it returns a list of files. How to do it to return only first file and first line in it?

Sheen
  • 3,333
  • 5
  • 26
  • 46

1 Answers1

23

Just use the Select-Object command to return the first match. You don't need to use Get-ChildItem since you can specify the path parameter in Select-String. The Select-String command returns MatchInfo object which contains the matching line and also the name of the file.

$m = Select-String -Pattern get -Path *.ps1 -list -SimpleMatch | select-object -First 1
$m.Line
$m.Path
bouvierr
  • 3,563
  • 3
  • 27
  • 32