Base64 Encode & Decode in PowerShell
Encode or decode below — it runs locally in your browser — then grab the canonical PowerShell code. PowerShell has no base64 command, so you reach into .NET: [Convert]::ToBase64String() over a byte[]. The one trap to avoid is the encoding — use UTF8, not Unicode.
Encode and decode a string
The pattern is string → UTF8.GetBytes() → ToBase64String() to encode, and the mirror image to decode:
# Encode
$b64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("Hello, world!"))
$b64 # SGVsbG8sIHdvcmxkIQ==
# Decode
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($b64))
# Hello, world!
Tip: use [Text.Encoding]::UTF8, not ::Unicode. Unicode in .NET means UTF-16, which produces a different (longer) Base64 string that other languages and tools will not decode the same way.
The UTF-16 trap
You will see this shorter one-liner all over the internet — and it gives the wrong answer for cross-platform use:
# UTF-16LE — produces DIFFERENT, longer Base64
[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("Hello, world!"))
# SABlAGwAbABvACwAIAB3AG8AcgBsAGQAIQA=
# UTF-8 — matches Python, Node, and the Linux base64 command
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("Hello, world!"))
# SGVsbG8sIHdvcmxkIQ==
[Text.Encoding]::Unicode is UTF-16 little-endian: every ASCII character gets a trailing 00 byte, so H becomes the pair 48 00. Those interleaved null bytes are what create the distinctive ...A...A...A padding pattern and roughly double the length. Many Windows tutorials use ::Unicode for legacy reasons (and because powershell.exe -EncodedCommand genuinely requires UTF-16). For interoperability with everything else, match UTF8.
Encode a file
For files, skip text encoding entirely — read the raw bytes and encode them. [IO.File]::ReadAllBytes works on every PowerShell version:
$b64 = [Convert]::ToBase64String([IO.File]::ReadAllBytes("C:\path\file.png"))
# As a data URI for HTML/CSS:
$uri = "data:image/png;base64," + $b64
Frequently asked questions
How do I Base64 encode a string in PowerShell?
Get UTF-8 bytes, then encode: [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("Hello, world!")) returns SGVsbG8sIHdvcmxkIQ==.
How do I Base64 decode in PowerShell?
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($b64)). Strip whitespace/newlines first if the input is wrapped — FromBase64String is strict.
Why does my PowerShell Base64 differ from other tools?
You are likely using [Text.Encoding]::Unicode (UTF-16), which adds a null byte after each ASCII character and gives a longer, different string. Switch to [Text.Encoding]::UTF8 to match Python, Node, and the Linux base64 command.
How do I Base64 encode a file?
Encode the raw bytes directly: [Convert]::ToBase64String([IO.File]::ReadAllBytes("C:\path\file.png")).
Need image, file, or URL-safe modes?
The main base64.dev tool handles text, images, files, and URL-safe Base64 with auto-detect.
Open base64.dev →