When you spend as much time as I do writing scripts, it doesn't matter how sensibly you store and name them, you're always going to have situations where you know you've already written a snippet of code that does something, but don't know which file it's in. Windows' built in search does a fairly decent job of telling you which files contain a string of text, but you then have to open them to search again inside for the actual section you're looking for.
It's fine being able to locate what you're looking for, but when you've got a good sized body of work that you're searching through, especially if you didn't author it all yourself, then it's nice to be able to see the context that your search result occurs in. That's where Select-String is working wonders for me.
Select-String's -context parameter is used to define the number of lines either side of the search results that you want to include. The help says:
Captures the specified number of lines before and after the line with the match. This allows you to view the match in context.
If you enter one number as the value of this parameter, that number determines the number of lines captured before and after the match. If you enter two numbers as the value, the first number determines the number of lines before the match and the second number determines the number of lines after the match.
In the default display, lines with a match are indicated by a right angle bracket (ASCII 62) in the first column of the display. Unmarked lines are the context.
Typically I only specify one integer, either 2 or 3, to show 2 or 3 lines either side of the match, but it's nice to have the ability to say I want 1 line before and 6 after, or whatever.
So, if I know that we used the Set-QADuser cmdlet to do a particular task, I might do this:
select-string -path c:\scripts\*.ps1 -pattern "set-qaduser" -context 2
The results that I get back would be like this (trimmed to fit this page layout, but I'm sure you get the idea):
Of course there's more to Select-String than that. Firstly, the pattern that you're searching for, by default, is a regular expression, so you can do some really complex searches if you wish (to do a simple match for a string, just use the -simplematch parameter).
Being PowerShell, the results of your Select-String search aren't just text on the screen, they're rich MatchInfo objects that you can pass down the pipeline and do things with. In the results above, you can see the filename, line number and the content of that line - those are all seperate properties of the MatchInfo object, so say you wanted to do a complex RegEx search for some content in a bunch of files, then copy those files somewhere else and send an email to your colleague saying "Hey, check out examples of where we use Set-QADuser; I've copied them to our team folder and you want to look at these lines in the files..." it would be really easy.
I think Select-String is a pretty useful tool and if you haven't taken a proper look at it before, I encourage you to do so.