I often need to search through logs to find errors and other troubleshooting information. In the Linux world, this often means using grep
. In PowerShell, you can use Get-Content
and Where-Object
.
Get-Content mylogfile.txt | Where-Object { $_.Contains("ERROR") }
The code above will display any lines in mylogfile.txt that contain the text “ERROR.”
If you want to know how many lines contain “ERROR,” simply wrap the code above in parentheses and add “.Count
” like this:
@( Get-Content mylogfile.txt | Where-Object { $_.Contains("ERROR") } ).Count
In a case where the text “ERROR” only occurs once in the file, the Where-Object
cmdlet would return a string value containing the line in which the text was found. If there is more than one occurrence, it would return an array of the lines in which the text was found.
Because of this difference in the way the answer is returned, the “@” prior to the opening parenthesis is necessary, to force the answer to be returned as an array. For our purposes, we need it to be an array so that we can perform the Count
operation.
Dude the script is working but i need help.
I got a few strings that have the word multiple times in the same line, with this script it only counts as if there was one word matching.
Can you please help me?
Just to clarify – are you trying to get a count of how many times the word occurs in a string? What’s the ultimate goal, so I can tailor my answer?
Why not use a “select-string”? does where-object allow for phrases? I’m trying to perform an if statement if a log file contains a certain phrase, any suggestions?
First – apologies for my late reply.
My knee-jerk reaction was going to be “Select-String wasn’t introduced until PowerShell 3,” but it appears that Get-Content was also introduced in PowerShell 3. Truth be told, I’d only worked with the Get-Content/Where-Object solution
Get-Content & Where-Object is a quick way to simply get the content of the lines in question, but that can be done with Select-String as well – you just have to work with “MatchInfo” object to get it the way that you want it.
I guess the exact solution to your question would depend on exactly what you’re looking to do. Are you saying you want your script to take some action if the log contains a phrase? Or are you trying to select all of the lines that contain the phrase? Or both?
If it’s a simple “found the phrase, do something,” then this would work:
If ( Select-String -Pattern “my phrase” -Path C:\mylogfile.txt -Quiet ) { #Do something }
If you’re trying to capture all of the lines that contain a phrase:
$matches = Select-String -Pattern “my phrase” -Path C:\mylogfile.txt
The line contents can be returned with:
$matches.Lines
Or you can get a list of the matching files (if your path was a wildcard):
$matches.Path
Of you could loop through the matches and do a little processing:
ForEach ( $match in $matches) { Write-Output “The file $($match.Path) contains $($match.Line)” }
Thankyou ! worked for me
Hi,
Thanks for the details here, want to know how to handle case insensitivity. command given by you is to detect “ERROR” what if I need to find Error or error or ERROR.
Try this:
Get-Content ~\Desktop\mylogfile.txt | Where-Object { $_ -Like "*ERROR*" }