Quickly Finding Scripts

by Aug 20, 2015

To quickly locate a PowerShell script anywhere in your MyDocuments folder, take a look at this Find-Script function:

#requires -Version 3
function Find-Script
{
  param
  (
    [Parameter(Mandatory = $true)]
    $SearchPhrase,
    $Path = [Environment]::GetFolderPath('MyDocuments')
  )

  Get-ChildItem -Path $Path  -Filter *.ps1 -Recurse -ErrorAction SilentlyContinue |
  Select-String -Pattern $SearchPhrase -List |
  Select-Object -Property Path, Line |
  Out-GridView -Title "All Scripts containing $SearchPhrase" -PassThru |
  ForEach-Object -Process {
    ise $_.Path
  }
}

Run it like this:

Find-Script 'childitem' 

This would return a list of all PowerShell scripts in your user profile that contain the search word. When you select scripts in a grid view window and click OK, they are automatically opened in the PowerShell ISE.

To set a different search base, use the –Path parameter. This way, you can search your USB media or some network location just as easily.

Twitter This Tip! ReTweet this Tip!