Finding Scripts by Keyword

by Sep 30, 2013

With an increasing number of PowerShell scripts on your hard drive, it can become hard to find the script you are looking for. Here's a helper function called Find-Script. Simply submit a keyword, and PowerShell will locate any script within your user profile that has your keyword anywhere inside it.

The result displays in a grid view window, and you can then optionally click and select the file(s) you want to open in the ISE editor.

function Find-Script
{
    param
    (
        [Parameter(Mandatory=$true)]
        $Keyword,

        $Maximum = 20,
        $StartPath = $env:USERPROFILE
    )

    Get-ChildItem -Path $StartPath -Filter *.ps1 -Recurse -ErrorAction SilentlyContinue |
      Select-String -SimpleMatch -Pattern $Keyword -List |
      Select-Object -Property FileName, Path, Line -First $Maximum |
      Out-GridView -Title 'Select Script File' -PassThru |
      ForEach-Object { ise $_.Path }
} 

By default, Find-Script returns only the first 20 scripts that meet your request. With the parameters -Maximum and -StartPath, you can change both the maximum count and the search location.

Twitter This Tip! ReTweet this Tip!