How to search a string in multiple files and return the names of files in Powershell?

后端 未结 11 1815
野的像风
野的像风 2020-11-30 15:57

I have started learning powershell a couple of days ago, and I couldn\'t find anything on google that does what I need so please bear with my question.

I have been a

相关标签:
11条回答
  • 2020-11-30 16:23

    This will display the path, filename and the content line it found that matched the pattern.

    Get-ChildItem -Path d:\applications\*config -recurse |  Select-String -Pattern "dummy" 
    
    0 讨论(0)
  • 2020-11-30 16:34

    This should give the location of the files that contain your pattern:

    Get-ChildItem -Recurse | Select-String "dummy" -List | Select Path
    
    0 讨论(0)
  • 2020-11-30 16:34

    Pipe the content of your

    Get-ChildItem -recurse | Get-Content | Select-String -pattern "dummy"
    

    to fl *

    You will see that the path is already being returned as a property of the objects.

    IF you want just the path, use select path or select -unique path to remove duplicates:

    Get-ChildItem -recurse | Get-Content | Select-String -pattern "dummy" | select -unique path
    
    0 讨论(0)
  • 2020-11-30 16:34

    I modified one of the answers above to give me a bit more information. This spared me a second query later on. It was something like this:

    Get-ChildItem `
            -Path "C:\data\path" -Filter "Example*.dat" -recurse | `
        Select-String -pattern "dummy" | `
        Select-Object -Property Path,LineNumber,Line | `
        Export-CSV "C:\ResultFile.csv"
    

    I can specify the path and file wildcards with this structures, and it saves the filename, line number and relevant line to an output file.

    0 讨论(0)
  • 2020-11-30 16:37

    There are a variety of accurate answers here, but here is the most concise code for several different variations. For each variation, the top line shows the full syntax and the bottom shows terse syntax.

    Item (2) is a more concise form of the answers from Jon Z and manojlds, while item (1) is equivalent to the answers from vikas368 and buygrush.

    1. List FileInfo objects for all files containing pattern:

      Get-ChildItem -Recurse filespec | Where-Object { Select-String pattern $_ -Quiet }
      ls -r filespec | ? { sls pattern $_ -q }
      
    2. List file names for all files containing pattern:

      Get-ChildItem -Recurse filespec | Select-String pattern | Select-Object -Unique Path
      ls -r filespec | sls pattern | select -u Path
      
    3. List FileInfo objects for all files not containing pattern:

      Get-ChildItem -Recurse filespec | Where-Object { !(Select-String pattern $_ -Quiet) }
      ls -r filespec | ? { !(sls pattern $_ -q) }
      
    4. List file names for all files not containing pattern:

      (Get-ChildItem -Recurse filespec | Where-Object { !(Select-String pattern $_ -Quiet) }).FullName
      (ls -r filespec | ? { !(sls pattern $_ -q) }).FullName
      
    0 讨论(0)
  • 2020-11-30 16:37

    To keep the complete file details in resulting array you could use a slight modification of the answer posted by vikas368 (which didn't seem to work well with the ISE autocomplete):

    Get-ChildItem -Recurse | Where-Object { $_ | Select-String -Pattern "dummy" }
    

    or in short:

    ls -r | ?{ $_ | Select-String -Pattern "dummy" }
    
    0 讨论(0)
提交回复
热议问题