Skip to main content

Converts a file to utf8 encoding.

<#
.SYNOPSIS
    Converts a file to utf8 encoding.

.INPUT
    Files that are encoded some other way.

.EXAMPLE
    Find-Utf16 | ConvertTo-Utf8
#>
filter ConvertTo-Utf8 {
    $lines = [System.IO.File]::ReadAllLines($_)
    [System.IO.File]::WriteAllLines($_, $lines)
}

<#
.SYNOPSIS
    Find files that are utf16 encoded.

.PARAMETER Path
    A list of paths or masks for files to check.

.EXAMPLE
    Find-Utf16
#>
function Find-Utf16($Path = @('*.yaml', '*.cs', '*.xml')) {
    foreach ($file in (Get-ChildItem -Recurse -Path $Path)) {
        $bytes = [System.IO.File]::ReadAllBytes($file.FullName)
        if ($bytes[0] -eq 255 -and $bytes[1] -eq 254) {
            $file
        }
    }
}