Skip to content

Instantly share code, notes, and snippets.

@pcrockett-pathway
Last active April 10, 2019 14:23
Show Gist options
  • Select an option

  • Save pcrockett-pathway/a5756720d4c039415e33af81d282be79 to your computer and use it in GitHub Desktop.

Select an option

Save pcrockett-pathway/a5756720d4c039415e33af81d282be79 to your computer and use it in GitHub Desktop.
Get the encoding for a file
[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