Public/Get-SpecTextInFile.ps1
Function Get-SpecTextInFile { <# .SYNOPSIS Searches for text in a file. .DESCRIPTION The Get-SpecTextInFile function searches for specified text within a file. It provides options for both exact and partial matches. .PARAMETER FilePath Specifies the path to the file to be searched. .PARAMETER toFind Specifies the text to be searched for within the file. .PARAMETER ExactMatch Indicates whether to perform an exact match. If specified, the function will search for the exact occurrence of the specified text. By default, the function performs a partial match. .EXAMPLE Get-SpecTextInFile -FilePath 'C:\example.txt' -toFind 'searchtext' Searches for the text 'searchtext' within the file 'C:\example.txt' using a partial match. .EXAMPLE Get-SpecTextInFile -FilePath 'C:\example.txt' -toFind 'exactmatch' -ExactMatch Searches for the exact occurrence of the text 'exactmatch' within the file 'C:\example.txt'. .NOTES Author: owen.heaume Version: 1.0.0 #> [cmdletbinding()] param( [Parameter(Mandatory = $true)] [string]$FilePath, [Parameter(Mandatory = $true)] [string]$toFind, [switch]$ExactMatch # Switch parameter for exact match ) Begin { $encoding = "UTF8" } process { $content = Get-Content -Path $FilePath -Encoding $encoding if ($ExactMatch) { # Perform exact match if ($content -contains $toFind) { return $true } else { return $false } } else { # Perform case-insensitive match with regular expression $regex = [regex]::Escape($toFind) $pattern = "\b$regex\b" if ($content -match $pattern) { return $true } else { return $false } } } } |