Last active
April 10, 2019 14:23
-
-
Save pcrockett-pathway/a5756720d4c039415e33af81d282be79 to your computer and use it in GitHub Desktop.
Get the encoding for a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [CmdletBinding()] | |
| param( | |
| [Parameter(Mandatory=$True, ValueFromPipeline=$True)] | |
| [string]$Path | |
| ) | |
| $ErrorActionPreference = "Stop" | |
| Set-StrictMode -Version 5.0 | |
| $Path = (Resolve-Path $Path).ProviderPath | |
| $reader = [IO.StreamReader]::new($Path) | |
| try { | |
| # Reading a single character from the file will cause the StreamReader to | |
| # do encoding detection | |
| $reader.Read() | Out-Null | |
| $reader.CurrentEncoding | |
| } finally { | |
| $reader.Close() | |
| $reader.Dispose() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment