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

后端 未结 11 1816
野的像风
野的像风 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:38

    If you search into one directory, you can do it:

    select-string -Path "c:\temp\*.*" -Pattern "result"  -List | select Path
    
    0 讨论(0)
  • 2020-11-30 16:42

    This will display a list of the full path to each file that contains the search string:

    foreach ($file in Get-ChildItem | Select-String -pattern "dummy" | Select-Object -Unique path) {$file.path}
    

    Note that it doesn't display a header above the results and doesn't display the lines of text containing the search string. All it tells you is where you can find the files that contain the string.

    0 讨论(0)
  • 2020-11-30 16:47
    Get-ChildItem -r | ? {$_.psiscontainer -eq $false} | ? {gc $_.pspath |select-string -pattern "dummy"}
    

    This will give you the full details of all files

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

    With PowerShell, go to the path where your files are and then type this command and replace ENTER THE STRING YOU SEARCH HERE (but keep the double quotes):

    findstr /S /I /M /C:"ENTER THE STRING YOU SEARCH HERE" *.*
    

    Have a nice day

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

    This is how I would do it, you don't need get-content:

    ls -r | Select-String dummy | select line,path
    

    or

    ls -r | Select-String dummy | fl *
    

    To see what the different properties are...

    This is faster. The second argument is -filter:

    ls -r . *.bat | select-string netsh
    
    ls -r . -filter *.bat | select-string netsh
    
    0 讨论(0)
提交回复
热议问题