select-string how to only return first match line in first file select-string how to only return first match line in first file powershell powershell

select-string how to only return first match line in first file


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