Inspect byte signatures, validate UTF-8, and explicitly transcode known source encodings.

Sheet: 14 of 25 | Platform: Windows 10/11 | Shell: PowerShell 7.x and Windows PowerShell 5.1 (unless marked otherwise) | Editorial review: November 24, 2026

PowerShell cheat sheet index

Examples use the MGU project root and illustrative staging, backup, and download paths. Replace them with existing, approved paths as needed. Unless explicitly marked as a continuation, examples are independent. Read commands before running them; commands that write or modify files can change existing data.

Quick commands

1. Identify the current shell version

$PSVersionTable | Select-Object PSVersion, PSEdition

2. Inspect the first bytes of a file

$bytes = [IO.File]::ReadAllBytes('C:\MGU-Staging\article.html')
($bytes | Select-Object -First 4 | ForEach-Object { '{0:X2}' -f $_ }) -join ' '

3. Validate that bytes can decode as strict UTF-8

$utf8 = [Text.UTF8Encoding]::new($false, $true)
try {
    $null = $utf8.GetString([IO.File]::ReadAllBytes('C:\MGU-Staging\article.html'))
    'Valid UTF-8 byte sequence'
} catch { 'Read or UTF-8 decoding failed: ' + $_.Exception.Message }

4. Write UTF-8 without BOM (PowerShell 7)

Set-Content -LiteralPath 'C:\MGU-Staging\notes.txt' -Value 'Example text' -Encoding utf8NoBOM

5. Write UTF-8 without BOM using .NET (5.1 and 7)

[IO.File]::WriteAllText('C:\MGU-Staging\notes.txt', 'Example text', [Text.UTF8Encoding]::new($false))

6. Convert a confirmed Windows-1252 file to a new UTF-8 file

$source = 'C:\MGU-Staging\legacy.html'
$target = 'C:\MGU-Staging\converted.html'
if (Test-Path -LiteralPath $target) { throw 'Target exists.' }
# On PowerShell 7, register the code-page provider if needed.
if ($PSVersionTable.PSEdition -eq 'Core') {
    [Text.Encoding]::RegisterProvider([Text.CodePagesEncodingProvider]::Instance)
}
$legacy = [Text.Encoding]::GetEncoding(1252)
$text = $legacy.GetString([IO.File]::ReadAllBytes($source))
[IO.File]::WriteAllText($target, $text, [Text.UTF8Encoding]::new($false))

Operational notes

EF BB BF indicates a UTF-8 BOM; FF FE commonly indicates UTF-16LE (check four bytes for UTF-32LE); FE FF indicates UTF-16BE. A missing BOM does not establish encoding; valid UTF-8 bytes do not prove intended text. Windows PowerShell 5.1 -Encoding UTF8 writes a BOM; PowerShell 7 UTF8 writes no BOM. Convert only confirmed source encodings and inspect rendered text.

Official documentation