One major disadvantage for Windows administrators is that they don’t get by default the same breadth of administrative tools which appear on pretty much any UNIX server.  One of these is grep.

There is a reasonable equivalent for one of the most common grep features—searching within text files for a particular regular expression.  You can use the Select-String cmdlet to look for text in a particular file.

To do a recursive search for the word “Discovery” in the current directory and all subdirectories, you would issue the following command:

Get-ChildItem - recurse | Select-String -pattern "Discovery";

To search in the current directory for files with a .sql extension and which contain “BEGIN TRAN” (to begin a transaction), you could run something like:

Select-String *.sql -pattern "BEGIN TRAN"

One of the examples provided by the Select-String help lets you parse the latest event log entries, looking for error messages. Given how slow the Event Log plugin can be, this can be a very good thing.

Get-EventLog -logname Application -newest 100 | Select-String -inputobject { $_Message } -pattern "Failed"

Unfortunately, it’s not as easy to do certain things as it is in grep, but at least it accepts piped input… And when you’re in an environment in which you can’t get one of the many Windows grep replacements.


One thought on “Powershell’s Select-String

  1. hello!,I love your writing so much! share we keep in touch more about your post on AOL?
    I need a specialist on this area to resolve my problem.
    Maybe that is you! Having a look forward to look you.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s