# fix-bom.ps1 - ensure every .ps1 in this repo is UTF-8 WITH BOM. # Windows PowerShell 5.1 reads BOM-less .ps1 as ANSI/GBK, which corrupts non-ASCII # comments and can silently break parsing (observed: variables becoming empty). # ASCII-only on purpose: this file must survive being saved without a BOM. param([string]$Root) $ErrorActionPreference = 'Stop' if (-not $Root) { $Root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) } $utf8Bom = New-Object System.Text.UTF8Encoding($true) $fixed = 0 Get-ChildItem $Root -Recurse -File -Filter *.ps1 | Where-Object { $_.FullName -notmatch '\\\.cache\\' } | ForEach-Object { $bytes = [System.IO.File]::ReadAllBytes($_.FullName) $hasBom = $bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF if (-not $hasBom) { $text = [System.IO.File]::ReadAllText($_.FullName, [System.Text.Encoding]::UTF8) [System.IO.File]::WriteAllText($_.FullName, $text, $utf8Bom) Write-Host "[bom] + $($_.Name)" $script:fixed++ } } Write-Host "[bom] done, fixed=$fixed"