PoshCode Archive  Artifact [da1a4de495]

Artifact da1a4de495ee320782fa749ad3b1046d6ea889e1556172210b65d30341ca84f1:

  • File Get-FileEncoding.ps1 — part of check-in [794cfd082d] at 2018-06-10 13:22:35 on branch trunk — Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM). Output strings are determined from the possible values in the System.Text.Encoding enumeration (ASCII, BigEndianUnicode, Default, Unicode, UTF32, UTF7, UTF8) and are valid values for the “-encoding” parameter in cmdlets like Set-Content, Out-File, etc. (user: RyanFisher size: 2237)

# encoding: ascii
# api: powershell
# title: Get-FileEncoding
# description: Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM).  Output strings are determined from the possible values in the System.Text.Encoding enumeration (ASCII, BigEndianUnicode, Default, Unicode, UTF32, UTF7, UTF8) and are valid values for the “-encoding” parameter in cmdlets like Set-Content, Out-File, etc.
# version: 0.1
# type: script
# author: RyanFisher
# license: CC0
# function: Get-FileEncoding
# x-poshcode-id: 3231
# x-archived: 2012-02-13T11:11:55
# x-published: 2012-02-11T14:10:00
#
#
<#
.SYNOPSIS
Gets file encoding.
.DESCRIPTION
The Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM).
Based on port of C# code from http://www.west-wind.com/Weblog/posts/197245.aspx
.EXAMPLE
Get-ChildItem  *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'}
This command gets ps1 files in current directory where encoding is not ASCII
.EXAMPLE
Get-ChildItem  *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'} | foreach {(get-content $_.FullName) | set-content $_.FullName -Encoding ASCII}
Same as previous example but fixes encoding using set-content
#>
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.count -ge 0)
    {
        if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf )
        { Write-Output 'UTF8' }
        elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff)
        { Write-Output 'BigEndianUnicode' }
        elseif ($byte[0] -eq 0xff -and $byte[1] -eq 0xfe)
        { 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' }
    }
}