Get-StringfromFiles.psm1
<#
.Synopsis Search for the given string in all the files withing the given folder. .Description Search for the given string in all the files withing the given folder. .Parameter Path Path where you want to search the string. .Parameter Pattern Pattern or string you wnat to search. .Switch OpenFiles Select this switch if you want to open the files which contains the matching string. .Example # Search for the given string in all the files withing the given folder path. Get-StringfromFiles -Path D:\Logs\ -Pattern "SQL Log Entry: Failed" .Example # Search for the given string in all the files withing the given folder path and gives the option to open the files. Get-StringfromFiles -Path D:\Logs\ -Pattern "SQL Log Entry: Failed" -OpenFiles #> #------------------------------------------------------------------------------ # # THIS MODULE AND ANY ASSOCIATED INFORMATION ARE PROVIDED 'AS IS' WITHOUT # WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT # LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS # FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR # RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER. # #------------------------------------------------------------------------------ Function Get-StringfromFiles () { Param( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [String]$Path, [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [String]$Pattern, [Parameter(Mandatory=$false)] [Switch]$OpenFiles ) #Get Search Path $P2 = Get-ChildItem -Path $Path -Recurse | where {$_.PSIsContainer -eq $false} # Search the String & Putting the results in a variable $result = Select-String -Pattern $Pattern -Path $p2.fullname -AllMatches #Output $result | FL -Property Path,Pattern,LineNumber,Filename If($result -eq $null){ Write-Host "No entry found" -ForegroundColor Red} If($OpenFiles -eq $true){ # Opening the file If($result.count -gt 100){ $conf = [System.Windows.MessageBox]::Show('There are 100+ files, do you want to open them all?, Select Yes to open, No to abort','User Confirmation','YesNo') If ($conf -ne 'Yes'){Break} } $filestoOpen = $result | Out-GridView -Title 'Select Files you want to open' -OutputMode Multiple $filestoOpen | ForEach-Object {Start-Process -FilePath $_.path} } }# Func Close Export-ModuleMember -Function Get-StringfromFiles |