Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell – Get Text Encoding Type

Posted on June 16, 2020August 17, 2024 By David Kittell
function Get-Encoding
{
  param
  (
    [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
    [Alias('FullName')]
    [string]
    $Path
  )

  process 
  {
    $bom = New-Object -TypeName System.Byte[](4)
        
    $file = New-Object System.IO.FileStream($Path, 'Open', 'Read')
    
    $null = $file.Read($bom,0,4)
    $file.Close()
    $file.Dispose()
    
    $enc = [Text.Encoding]::ASCII
    if ($bom[0] -eq 0x2b -and $bom[1] -eq 0x2f -and $bom[2] -eq 0x76) 
      { $enc =  [Text.Encoding]::UTF7 }
    if ($bom[0] -eq 0xff -and $bom[1] -eq 0xfe) 
      { $enc =  [Text.Encoding]::Unicode }
    if ($bom[0] -eq 0xfe -and $bom[1] -eq 0xff) 
      { $enc =  [Text.Encoding]::BigEndianUnicode }
    if ($bom[0] -eq 0x00 -and $bom[1] -eq 0x00 -and $bom[2] -eq 0xfe -and $bom[3] -eq 0xff) 
      { $enc =  [Text.Encoding]::UTF32}
    if ($bom[0] -eq 0xef -and $bom[1] -eq 0xbb -and $bom[2] -eq 0xbf) 
      { $enc =  [Text.Encoding]::UTF8}
        
    [PSCustomObject]@{
      Encoding = $enc
      Path = $Path
    }
  }
}

Get-Encoding ~\Desktop\text.txt

Reference: https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/get-text-file-encoding

Originally Posted on June 16, 2020
Last Updated on August 17, 2024
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code PowerShell ASCIIText EncodingUnicodeutfutf32utf8

Post navigation

Previous post
Next post

Related Posts

Read Online Content

Posted on November 6, 2013October 26, 2015

With this example I only want an IP address so it’s rather clean and easy but you can essentially put any public webpage in place of “http://kittell.net/onlyip.php” System.Net.WebClient wc = new System.Net.WebClient(); byte[] raw = wc.DownloadData("http://kittell.net/onlyip.php"); string webData = System.Text.Encoding.UTF8.GetString(raw); Response.Write(webData); Originally Posted on November 6, 2013Last Updated on October…

Read More

Android Contacts – Set Text Case

Posted on March 2, 2014October 26, 2015

UPDATE data SET data1 = lower(data1) WHERE mimetype_id IN ( SELECT _id FROM mimetypes WHERE mimetype LIKE "%email%" ); Originally Posted on March 2, 2014Last Updated on October 26, 2015 All information on this site is shared with the intention to help. Before any source code or program is ran…

Read More

Mac OSX Terminal – Get Mac Serial Number

Posted on June 17, 2016

A quick and easy way to get your serial number # Get Serial Number sSerialNumber=$(system_profiler SPHardwareDataType |grep "Serial Number (system)" |awk ‘{print $4}’ | cut -d/ -f1) echo $sSerialNumber All information on this site is shared with the intention to help. Before any source code or program is ran on…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories
  • Raspberry Pi - Remove Default Apps
  • PowerShell - Change Windows CD/DVD Drive Letter
©2025 David Kittell | WordPress Theme by SuperbThemes