functions/filesystem/Convert-ToUtf8Bom.ps1

function Convert-ToUtf8Bom {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias("FullName")]
        [string[]] $Path
    )

    process {
        if ($Path.Count -eq 1 -and $Path[0] -eq '.') {
            $Path[0] = (Resolve-Path -Path '.' -ErrorAction Stop).Path
        }

        foreach ($inputPath in $Path) {
            if (Test-Path $inputPath) {
                $files = @()

                if ((Get-Item $inputPath).PSIsContainer) {
                    $files += Get-ChildItem -Path $inputPath -Recurse -Filter *.ps1
                } else {
                    $files += Get-Item -Path $inputPath
                }

                foreach ($file in $files) {
                    if (-not (Test-IsUtf8WithBom -Path $file))
                    {
                        try {
                            $content = Get-Content -Path $file.FullName -Raw
                            $utf8BomEncoding = New-Object System.Text.UTF8Encoding($true)
                            [System.IO.File]::WriteAllText($file.FullName, $content, $utf8BomEncoding)

                            Write-Verbose "✔ Converted: $($file.FullName)"
                        } catch {
                            Write-Error "Error converting file $($file.FullName): $_"
                        }
                    } else {
                        Write-Verbose "File $($file.FullName) already has Unicode byte order mark (BOM)."
                    }
                }
            } else {
                Write-Warning "File not found: $inputPath"
            }
        }
    }
}