forked from jdhitsolutions/ISEScriptingGeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find-InFile.ps1
53 lines (43 loc) · 3.34 KB
/
Find-InFile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#Requires -version 3.0
Function Find-InFile {
[cmdletbinding()]
Param()
Set-StrictMode -Version Latest
Write-Verbose "Starting $($myinvocation.mycommand)"
#verify we are in the ISE
if ($host.name -match "ISE") {
$Title = "Find in Files"
#prompt for file types to search
$Prompt = "Enter a path and file types to search. Leave blank to cancel"
$Default = ".\*.ps1"
$path = New-Inputbox -prompt $prompt -title $Title -default $Default
if ($path) {
#prompt for what to search for
$Prompt = "What do you want to search for"
$Default= $Null
$find = New-Inputbox -prompt $prompt -title $Title -default $Default
#execute search
$results= Select-String -Pattern $find -Path $path |
Select Path,Filename,
@{Name="Line";Expression={$_.Line.Trim()}},LineNumber |
Out-Gridview -Title "Select one or more matching files" -OutputMode Multiple
#open files and jump to matching line
foreach ($item in $results) {
Write-Verbose ($item | out-string)
psedit $item.path
#give file a chance to open
start-sleep -Milliseconds 100
#get current files
$f = $psise.CurrentPowerShellTab.Files
#select the last one
$psise.CurrentPowerShellTab.Files.SelectedFile = $f[-1]
#set the cursor
$psise.CurrentPowerShellTab.files.SelectedFile.Editor.SetCaretPosition($item.linenumber,1)
}
}
}
else {
Write-Warning "This version only works in the PowerShell ISE"
}
Write-Verbose "Ending $($myinvocation.mycommand)"
}