PoshCode Archive  Artifact [ce0e08ee9f]

Artifact ce0e08ee9fd1b8d7bc154e17099ff2cfc9334c825d0fe01c64b9ab2339d2e0b3:

  • File Get-FileEncoding-UTF8BOM.ps1 — part of check-in [bdc391b53a] at 2018-06-10 14:21:12 on branch trunk — Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM). (user: Robert size: 1205)

# 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' }
}