I recently wrote a blog about how to find scripts where you had accomplished some specific task. That blog article was titled “How do I find that PowerShell Script where I did __________?”
What if you not only want to find those scripts, but also open them automatically in new tabs in the PowerShell ISE? It just so happens that you’re in luck:
1 2 3 4 5 6 | Get-ChildItem -Path 'C:\Scripts\*.ps1' -Recurse | Select-String -Pattern "1.." -SimpleMatch | Select-Object -Property Path -Unique | ForEach-Object { $psISE.CurrentPowerShellTab.Files.Add("$($_.Path)") } | Out-Null |
I went ahead and piped the results to Out-Null because I didn’t want anything returned at the command line. As you can see, there were three scripts that were automatically opened in new tabs in the PowerShell ISE:
The one-liner shown above was a direct copy and paste from the previously referenced blog article, but you could also expand the “Path” property to make it a little cleaner:
1 2 3 4 5 6 | Get-ChildItem -Path 'C:\Scripts\*.ps1' -Recurse | Select-String -Pattern "1.." -SimpleMatch | Select-Object -ExpandProperty Path -Unique | ForEach-Object { $psISE.CurrentPowerShellTab.Files.Add("$_") } | Out-Null |
These scripts could of course return a lot of results, more than you would want to try to open in new tabs in the ISE so you could only open them if say there’s no more than a dozen results, otherwise return a warning and the results at the command line:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $results = Get-ChildItem -Path 'C:\Scripts\*.ps1' -Recurse | Select-String -Pattern "1.." -SimpleMatch | Select-Object -ExpandProperty Path -Unique if ($results.count -le 12) { foreach ($result in $results) { $psISE.CurrentPowerShellTab.Files.Add("$result") } } else { Write-Warning -Message "There were $($results.count) results which is too many to open in new tabs." Write-Output $results } |
The magic of opening a script in a new tab in the PowerShell ISE is with this command:
1 | $psISE.CurrentPowerShellTab.Files.Add("ScriptName") |
µ
I did something similar. However, I used the Out-Gridview -passthru to display them for selection; I can select as many as I like. In the future, I would also like to parse the first line of the function’s help and display that as well.
function Get-FunctionFileName
{
[CmdletBinding()]
param ($Name)
$Start = $PWD
$ArrayofPaths = @( $env:PSModulePath -split ‘;’)
$Collection = @()
$Name = $Name -replace ‘\.\\’, ”
$Name = $Name -replace ‘\.ps1′,”
foreach ($dir in $ArrayofPaths)
{
Set-Location -Path $dir
$Collection += Get-ChildItem -Recurse -File -path “*$Name*.ps1”
}
$Results = $Collection | Select-Object -Property LastWriteTime, Length, Fullname, @{N=’Module’;E={Split-Path -Path $_.Directory -Leaf }}
$Selection = $Results | Out-GridView -PassThru -Title ‘Boss, Genius Idea! Look at these functions! ‘
if ( $Selection -eq $null ) {
break
}
foreach ( $item in $Selection ) {
psEdit $item.FullName
}
}