# encoding: ascii
# api: powershell
# title: Get-FileEncoding UTF8BOM
# description: Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM).
# version: 0.1
# type: function
# author: Robert
# license: CC0
# function: Get-FileEncoding
# x-poshcode-id: 6811
# x-archived: 2017-03-25T17:18:54
# x-published: 2017-03-21T15:37:00
#
#
<#
 $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf Gets files that are UTF-8 with BOM
#>
function Get-FileEncoding
{
    [CmdletBinding()] Param (
     [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] [string]$Path
    )

    [byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path

    if($byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf)
    {Write-Output 'UTF-8-BOM'}
    elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff)
    { Write-Output 'Unicode' }
    elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff)
    { Write-Output 'UTF32' }
    elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76)
    { Write-Output 'UTF7'}
    else
    { Write-Output 'ASCII or UTF-8' }
}
