Select-string, as soon as returning the first matching string in the first file

I am looking for a directory for the template, switches -SimpleMatch -List. But it returns a list of files. How to do this to return only the first file and the first line?

+4
source share
1 answer

Just use the command Select-Objectto return the first match. You do not need to use Get-ChildItem, since you can specify the path parameter in Select-String. The command Select-Stringreturns an object MatchInfothat contains the corresponding string, as well as the file name.

$m = Select-String -Pattern get -Path *.ps1 -list -SimpleMatch | select-object -First 1
$m.Line
$m.Path
+6
source

All Articles