Public/Find-specStringInFile.ps1
function Find-specStringInFile { <# .SYNOPSIS Searches for a specified text pattern in one or more files and returns the result. .DESCRIPTION The `Find-specStringInFile` function searches for a specified text pattern in one or more files. It returns a custom object for each file, indicating whether the text pattern was found, and if so, providing the line number and the text containing the pattern. .PARAMETER FilePath The path(s) of the file(s) to search. This parameter supports pipeline input by property name or by value. .PARAMETER TextToFind The text pattern to search for within the specified files. This parameter supports pipeline input by property name. .EXAMPLE Find-specStringInFile -FilePath "C:\TestFile1.txt" -TextToFind "keyword" This example searches for the text pattern "keyword" in the file "C:\TestFile1.txt". .EXAMPLE "C:\TestFile1.txt", "C:\TestFile2.txt" | Find-specStringInFile -TextToFind "keyword" This example searches for the text pattern "keyword" in multiple files "C:\TestFile1.txt" and "C:\TestFile2.txt", using pipeline input. .EXAMPLE Find-specStringInFile -FilePath "C:\TestFile1.txt", "C:\TestFile2.txt" -TextToFind "keyword" This example searches for the text pattern "keyword" in multiple files "C:\TestFile1.txt" and "C:\TestFile2.txt". .EXAMPLE $files = Get-ChildItem -Path "C:\Folder" -Filter "*.txt" | Select-Object -ExpandProperty FullName $files | Find-specStringInFile -TextToFind "keyword" This example searches for the text pattern "keyword" in all text files within the "C:\Folder" directory, using pipeline input. .INPUTS System.String[] The file path(s) to search, passed via the pipeline or directly as a parameter. System.String The text pattern to search for, passed directly as a parameter. .OUTPUTS System.Management.Automation.PSCustomObject An object containing the file path, status of the search (Present/Not present), line number, and the text containing the pattern. .NOTES Version: 1.0 - Initial release Author: owen.heaume #> [cmdletbinding()] param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string[]]$FilePath, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [String]$TextToFind ) process { foreach ($file in $filepath) { if ($result = Select-String -Path $file -Pattern $textToFind) { $result = $result -split ':' [pscustomobject]@{ FilePath = $file Status = 'Present' LineNumber = $result[2] Text = $result[3] } } else { [pscustomobject]@{ FilePath = $file Status = 'Not present' LineNumber = '' Text = '' } } } } } |