How to search a word in a file using PowerShell script

大城市里の小女人 提交于 2019-12-13 20:05:13

问题


I have a list of 350 folders and each folder has a file Access log. I need to search all 350 files under all 350 folders for a name "Hound" and display the name of the folders which contain the name "Hound" in their access log file. Below is my code, can someone help me with what should be added here to get the desired output, please?

   #List all the folders in C:\testfolder
   $folders = (Get-ChildItem -Path "C:\testfolder" | Where-Object{$_.Attributes -eq "Directory"} | Select Fullname)

   #looping all folders
   Foreach ($folder in $folders)
   {

   #Here I need to look for the word "Hound" inside the Access.log file and if the word is there, it should display the name of the $folder which has the word
   }

回答1:


Here's a fairly basic way to do this:

Get-ChildItem -Path d:\testfolder -Recurse | Select-String -Pattern "Hound"

If you need to make sure that only files called access.log are searched then specify a filter:

Get-ChildItem -Path d:\testfolder -Include "access.log" -Recurse | Select-String -Pattern "Hound"


来源:https://stackoverflow.com/questions/35520368/how-to-search-a-word-in-a-file-using-powershell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!