Searching through all those script files: Find-Script

If you are like me, you have built up a good amount of scripts over time and haven’t yet made it to the point of creating modules or other ways to handle all of them. I needed a way to be able to search through all these scripts to find the little gems that hide within.

I came up with this short and sweet script I can run quickly to find the tidbits I am looking for. The script will prompt for a keyword and then search recursively the path specified showing the output via Out-GridView.

Not a perfect solution but works very well. It could also be tweaked to search files other than *.ps1 easily enough.


function Find-Script
{
param
(
[Parameter(Mandatory=$true)]
[string]$Keyword
)
# Max results returned
$Maximum = 20
# Path to scripts directory
$StartPath = "E:\scripts"
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 'Script Files Found'
}
$keyword = Read-Host "Enter keyword to search for"
Find-Script $keyword
# Prompt to exit script
Write-Host "`n`nPress any key to exit – also closes grid view window…" -fore "Yellow"
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

view raw

FindScript.ps1

hosted with ❤ by GitHub

Leave a Reply

Your email address will not be published. Required fields are marked *