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?
            Asked
            
        
        
            Active
            
        
            Viewed 4.3k times
        
    1 Answers
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
- 
                    thanks. Also I find that combination of -List and -Quiet achieving same effect. – Sheen Aug 19 '14 at 12:21
