PoshCode Archive  Artifact [31cc05aa78]

Artifact 31cc05aa78d070ced1a82af23ff99e6f6048700c360512de8c1abb5c07599661:

  • File Get-FileEncoding.ps1 — part of check-in [a177e021d6] at 2018-06-10 14:03:41 on branch trunk — Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM). (user: Jason Poad size: 2782)

# encoding: ascii
# api: powershell
# title: Get-FileEncoding
# description: Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM).
# version: 1.0
# type: script
# author: Jason Poad
# license: CC0
# function: Get-FileEncoding
# x-poshcode-id: 5901
# x-archived: 2015-06-22T09:12:22
# x-published: 2015-06-19T14:13: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
.OUTPUT
  System.Text.Encoding
.EXAMPLE
  # This command gets ps1 files in current directory where encoding is not ASCII
  Get-ChildItem  *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {[string]$_.Encoding -ne 'System.Text.ASCIIEncoding'}
.EXAMPLE
  # Same as previous example but fixes encoding using set-content
  Get-ChildItem  *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {[string]$_.Encoding -ne 'System.Text.ASCIIEncoding'} | foreach {(get-content $_.FullName) | set-content $_.FullName -Encoding ASCII}
.NOTES
  Version History
  v1.0   - 2010/08/10, Chad Miller - Initial release
  v1.1   - 2010/08/16, Jason Archer - Improved pipeline support and added detection of little endian BOMs. (http://poshcode.org/2075)
  v1.2   - 2015/02/03, VertigoRay - Adjusted to use .NET's [System.Text.Encoding Class](http://goo.gl/XQNeuc). (http://poshcode.org/5724)
  v1.3   - 2015/06/19, Jason Poad - Fixed index error, variable spelling mistake, and added ValueFromPipeline modifier
.LINK
  http://goo.gl/bL12YV
#>
function Get-FileEncoding {
    [CmdletBinding()]
    param (
        [Alias("PSPath", "Fullname")]
        [Parameter(Mandatory = $True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName = $True)]
        [string]$Path
    )
 
    PROCESS {
        [Byte[]]$bom = Get-Content -path $Path -Encoding Byte -ReadCount 4 -TotalCount 4
        
        $encoding_found = $false

        foreach ($encoding in [System.Text.Encoding]::GetEncodings().GetEncoding()) {
            $preamble = $encoding.GetPreamble()
            if ($preamble) {
                foreach ($i in 0..($preamble.Length-1)) {
                    if ($preamble[$i] -ne $bom[$i]) {
                        break
                    } elseif ($i -eq $preamble.Length) {
                        $encoding_found = $encoding
                    }
                }
            }
        }

        if (!$encoding_found) {
            # $encoding_found = [System.Text.Encoding]::Default
            $encoding_found = [System.Text.Encoding]::ASCII
        }

        $encoding_found
    }
}