"Grep": Finding PowerShell Scripts with a Keyword

by Jul 10, 2009

Select-String can automatically search entire folders and scan all file contents for specific key words. All you do is to provide a path to scan, and of course a keyword to look for.

Let's check out how easy it is to find all PowerShell scripts dealing with a specific command. The next line finds all *.ps1 files in your Documents folder containing Get-WMIObject.

select-string -path $homeDocuments*.ps1 Get-WmiObject -list

Note the use of -list which lists each file only once. Without this switch parameter, you would get one result for each found keyword. Note also that select-string returns rich objects which you can format:

select-string -path $home*.ps1 Get-WmiObject -list | Format-Table Path