Quick Search in Log Files using PowerShell
If you need to read through large log files and search for specific keywords, there’s no need to write complex scripts but a simple three liner inside PowerShell can do all the work for you! The sample PowerShell code below, allows you to search through text files such as, log files in Windows 7 systems and outputs every line where a match of the given keyword is found.
- $filepath=”C:\Users\George\AppEventLog.txt”
- $word=”id:1003″
- Get-Content -path $filepath | ForEach-Object {if($_ -match $word){$_}}
Assuming that you want to search for the string ‘id:1003′ in a file called ‘AppEventLog.txt’ located at ‘c:\Users\George\‘. The first two lines assign string values to variables while, the last line is composed of two separate commands. The first part reads the contents of the text file using the Get-Content cmdlet. The second part takes the results of the Get-Content cmdlet and feeds it to the ForEach-Object cmdlet through the pipe connector. The ForEach-Object loop allows you to examine individual lines within the file and evaluate a condition using the ‘if’ statement.




