Skip to content

Instantly share code, notes, and snippets.

@Tiberriver256
Last active January 14, 2024 07:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tiberriver256/1709ffb4d32a426f61167c576c1ae4a3 to your computer and use it in GitHub Desktop.
Save Tiberriver256/1709ffb4d32a426f61167c576c1ae4a3 to your computer and use it in GitHub Desktop.
This is a tiny little PowerShell script that will find any files you have open in Notepad and present them in out-gridview with lines to help with searching. Large files it will prompt you for a pre-search term or a regex to make it usable in out-gridview. Super handy for when you get logs in Outlook and don't want to save them to somewhere spec…
$FilePaths = @()
$FilePaths = (Get-WmiObject win32_process -filter "name like 'notepad.exe'") |
foreach { ($_.commandline -split " ",2)[1] }
if($FilePaths.count -gt 1) {
$FilePath = $FilePaths | Out-GridView -PassThru -Title "Select the open file you would like to search"
} else {
$FilePath = $FilePaths
}
$FileSizeInMB = (Get-ChildItem -Path $FilePath).Length / 1mb
if($FileSizeInMB -lt 8){
$SearchResults = Select-String -Path $FilePath -Pattern ".*" | select linenumber,line | Out-GridView -PassThru
} else {
cls
Write-Host ""
$title = "Text Pre-Filtering"
$message = "This file is larger than 8MB. It is recommended to input a filter before using Out-GridView. Do you want to filter with a plain text simple search or a regular expression?"
$RegEx = New-Object System.Management.Automation.Host.ChoiceDescription "&Regular Expression", `
"Will prompt you for a regular expression to use to search the file"
$StringSearch = New-Object System.Management.Automation.Host.ChoiceDescription "&Simple Search", `
"Will prompt you for a simple string to match in the text"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($RegEx, $StringSearch)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
0 {
$Filter = Read-Host "Regular expression to use in the search"
$SearchResults = Select-String -Path $FilePath -Pattern $Filter | select linenumber,line | Out-GridView -PassThru
}
1 {
$Filter = Read-Host "Text to search / filter on"
$SearchResults = Select-String -Path $FilePath -Pattern $Filter -SimpleMatch | select linenumber,line | Out-GridView -PassThru
}
}
}
$SearchResults | clip
Write-Host $SearchResults
Write-Host "These search results have been saved to your clipboard" -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment