November 25th, 2015

PowerTip: Create Function List and Find PowerShell Files

Doctor Scripto
Scripter

SummaryUse Windows PowerShell to create a function list and find files.

Hey, Scripting Guy! Question How can I use Windows PowerShell to avoid repetitively typing “ls . –r *.ps1” to get a list of PowerShell files and then
           “ls . –r *.ps1 | sls function” to search the content of those files for a string?

Hey, Scripting Guy! Answer Add this function to your PowerShell profile:

function fps ($find) {    

    "Get-ChildItem . -Recurse *.ps1 $(if($find) { "| Select-String $find" })" |

        Invoke-Expression

}

Then, if you type `fps`, you’ll get a list of .ps1 files. To search for the string functions in each file, type `fps function`.

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.