Source/Public/Find-3LStringInFile.ps1

Function Find-3LStringInFile {

<#
 
.SYNOPSIS
    Find text in a file
 
.NOTES
    Author : Frits van Drie (3-Link.nl)
    Versions : 2021.09.23
 
.EXAMPLE
    FindStringInFile -pattern 'Project' -path "document.txt"
    searches document.txt in current folder for 'project' or 'Project'
 
.EXAMPLE
    FindStringInFile -pattern 'Project' -path "document.txt" -caseSensitive
    searches document.txt in current folder for 'Project'
 
.EXAMPLE
    gci "C:\Users\user\documents" -File | foreach { FindStringInFile -pattern 'project' -path $_.FullName }
    searches all users documents 'project' or 'Project'
 
#>



    [CmdletBinding()]

    param (

        [parameter(mandatory = $true)]
        [string]$pattern,

        [parameter(mandatory = $true)]
        [string]$path,

        [parameter(mandatory = $false)]
        [switch]$caseSensitive

    )

    begin {

        Write-Verbose "Start Function: $($MyInvocation.MyCommand)"

    }

    process {

        foreach ($file in $path) {

            try {

                $objfile = Get-item $path -ea Stop

                $result = $objFile | Select-String -Pattern $pattern -ea Stop -CaseSensitive:$caseSensitive

                Write-Output $result

            }

            catch {

                Write-Error "File not found: $path"

            }
        }

    }

    end {

        Write-Verbose "End Function: $($MyInvocation.MyCommand)"

    }

}