views:

847

answers:

5

What's the DOS FINDSTR equivalent for PowerShell? I need to search a bunch of log files for "ERROR".

+6  A: 

Here's the quick answer

gci -r -i *.log | select-string ERROR

I found it here which has a great indepth answer!

Monroecheeseman
A: 
if ($entry.EntryType -eq "Error")

Being Object Oriented, you want to test the property in question with one of the standard comparison operators you can find here.

I have a PS script watching logs remotely for me right now - some simple modification should make it work for you.

edit: I suppose I should also add that is a cmdlet built for this already if you don't want to unroll the way I did. Check out:

man Get-EventLog
Get-EventLog -newest 5 -logname System -EntryType Error
slipsec
+1  A: 

There's a website with a good article for this:

http://www.interact-sw.co.uk/iangblog/2006/06/03/pshfindstr

Hope this helps

Andrew

Andrew Taylor
+2  A: 

For example, find all instances of "#include" in the c files in this directory and all sub-directories.

gci -r -i *.c | select-string "#include"

gci is an alias for get-childitem

dpp
+3  A: 

Just to expand on Monroecheeseman's answer. gci is an alias for Get-ChildItem (which is the equivalent to dir or ls), the -r switch does a recursive search and -i means include.

Piping the result of that query to select-string has it read each file and look for lines matching a regular expression (the provided one in this case is ERROR, but it can be any .NET regular expression).

The result will be a collection of match objects, showing the line matching, the file, and and other related information.

Steven Murawski