Powershell Script to Search For a String in Multiple Log Files

Introduction

This article provides a Powershell Script to search for a specific string in multiple log files using a PowerShell script.

Need for this script

Consider a scenario where you have thousands of .log files and you need to find whether a specific string pattern is available or not in each of the log files. PowerShell becomes handy otherwise you may end up searching the string for days.

The following piece of code gets input from the user on the log files location path and the string to be searched for and loops through all the log files under the given location and searches for the specific string. An output file (FileContainingString.txt) with the details of the log file that has the specific string is generated and placed under the same location where the script is placed.

  1. $path = read-host "Enter the path for the log files "  
  2. $Searchstring = read-host "Enter the string to be searched "  
  3. $Logs = Get-ChildItem -path $path -recurse -include *.log  
  4. foreach($Log in $Logs)  
  5. {  
  6.     $StringExist = Select-String -Path $log.fullname -pattern $Searchstring  
  7.     if($StringExist)  
  8.     {  
  9.         write-host "String found in " $Log.name -fore green  
  10.         $log.name | out-file $scriptbase\FileContainingString.txt -append  
  11.     }  
  12.     else  
  13.     {  
  14.         write-host "String not found in " $Log.name -fore cyan  
  15.     }  
  16. }  
Complete Code
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\SearchStringPatch-$LogTime.rtf"  
  3.  
  4. # Add SharePoint PowerShell Snapin  
  5.   
  6.   
  7. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 
    {  
  8.     Add-PSSnapin Microsoft.SharePoint.Powershell  
  9. }  
  10.   
  11. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  12. Set-Location $scriptBase  
  13.  
  14. #Deleting any .rtf files in the scriptbase location  
  15. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  16. if($FindRTFFile)  
  17. {  
  18.     foreach($file in $FindRTFFile)  
  19.     {  
  20.         remove-item $file  
  21.     }  
  22. }  
  23.   
  24. $TestPath = test-path -path $scriptbase\FileContainingString.txt  
  25. if($testpath)  
  26. {  
  27.    remove-item $scriptbase\FileContainingString.txt  
  28. }  
  29.   
  30. start-transcript $logfile  
  31.   
  32. $path = read-host "Enter the path for the log files "  
  33. $Searchstring = read-host "Enter the string to be searched "  
  34. $Logs = Get-ChildItem -path $path -recurse -include *.log  
  35. foreach($Log in $Logs)  
  36. {  
  37.     $StringExist = Select-String -Path $log.fullname -pattern $Searchstring  
  38.     if($StringExist)  
  39.     {  
  40.         write-host "String found in " $Log.name -fore green  
  41.         $log.name | out-file $scriptbase\FileContainingString.txt -append  
  42.     }  
  43.     else  
  44.     {  
  45.         write-host "String not found in " $Log.name -fore cyan  
  46.     }  
  47. }  
  48. stop-transcript  
Execution procedure
  • Step 1: Download the script
  • Step 2: Navigate to the script path
  • Step 3: Execute the script



Conclusion

Thus this article outlines how to search for a specific string pattern in thousands of log files using a PowerShell script.


Similar Articles