PowerShell · 7 min read

Base64 decode in PowerShell.

PowerShell has no base64 cmdlet, but .NET does the job in one line. The catch is the text encoding: PowerShell's own -EncodedCommand uses UTF-16, so the decode that works for an API token gives gibberish for a script. Both are covered here, plus files and the security-analyst angle.

Updated 9 November 2026

Quick answer

Use [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($s)). If the string came from powershell -EncodedCommand (or -enc, -e), it is UTF-16LE, so use [Text.Encoding]::Unicode.GetString(...) instead. To save binary data, use [IO.File]::WriteAllBytes("C:\full\path\out.pdf", [Convert]::FromBase64String($s)).

The one-liner

$s = "SGVsbG8sIHdvcmxkIQ=="
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($s))
# Hello, world!

[Convert]::FromBase64String returns a byte[]. The GetString call turns those bytes into text using the encoding you name. These work in Windows PowerShell 5.1 and PowerShell 7 alike (the System. prefix is optional).

Whitespace and line breaks in the input are ignored. Anything else that isn't Base64, or a length that isn't a multiple of 4, throws a FormatException such as “The input is not a valid Base-64 string”.

UTF8 vs Unicode: pick the right encoding

The same bytes give very different text depending on the encoding you pass:

EncodingUse it forExample Base64
[Text.Encoding]::UTF8API responses, tokens, config values, Linux-generated dataSGVsbG8= → Hello
[Text.Encoding]::Unicode (UTF-16LE)-EncodedCommand, many Windows APIsSQBFAFgA → IEX
[Text.Encoding]::ASCIIOnly when you know it's 7-bit ASCIIReplaces other bytes with ?

A quick tell: UTF-16LE text that is mostly English has a zero byte after every character, which shows up in Base64 as a repeating A pattern (SQBFAFgA, JABjACAAPQAg...). If you see that, use Unicode.

Reading -EncodedCommand safely

powershell.exe -EncodedCommand runs a script supplied as Base64 of its UTF-16LE text. Administrators use it to avoid quoting problems. Attackers use it to hide commands from casual inspection, so it turns up constantly in incident response. Accepted spellings include -EncodedCommand, -enc, -ec and -e.

To read one, take the Base64 argument and decode it as Unicode. Decoding is safe: it only turns text into other text.

$b64 = "JABjACAAPQAgADEA"     # the argument after -enc
[Text.Encoding]::Unicode.GetString([Convert]::FromBase64String($b64))
# $c = 1
Don't run it to see what it does

Never pipe a decoded payload into Invoke-Expression (IEX) or paste it into a console to “see what happens”. Write it to a text file and read it. Do the analysis on an isolated machine if you suspect the sample is live.

Decoded payloads are often layered. Things to look for in the output:

  • FromBase64String( again: a second Base64 layer. Decode it the same way, though inner layers are often UTF-8.
  • IO.Compression.GzipStream or DeflateStream: the inner Base64 is compressed. Gzip data starts with H4sI in Base64.
  • A blob starting with TVqQ: that is a Windows executable (MZ header). Save it as bytes and hash it; do not open it.

Decoding a gzip-compressed inner layer:

$bytes = [Convert]::FromBase64String($inner)
$ms = New-Object IO.MemoryStream(,$bytes)
$gz = New-Object IO.Compression.GzipStream($ms, [IO.Compression.CompressionMode]::Decompress)
(New-Object IO.StreamReader($gz)).ReadToEnd()

To encode your own command for -EncodedCommand, reverse it: [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($cmd)).

Decode Base64 to a file

$b64 = Get-Content -Raw .\input.b64
[IO.File]::WriteAllBytes("$PWD\output.pdf", [Convert]::FromBase64String($b64))

Two details. Use -Raw so Get-Content returns one string instead of an array of lines. And give WriteAllBytes a full path: .NET resolves relative paths against the process's working directory, which is often not the folder PowerShell is showing you. $PWD fixes that.

On any Windows machine you can also use the built-in certutil:

certutil -decode input.b64 output.pdf

certutil accepts plain Base64 or text wrapped in -----BEGIN----- lines. Security tools often flag its use, because malware uses it too, so prefer the PowerShell version on monitored machines.

URL-safe and unpadded strings

FromBase64String only understands the standard alphabet and requires padding. Fix both first:

function ConvertFrom-Base64Url([string]$s) {
    $s = $s.Replace('-', '+').Replace('_', '/')
    switch ($s.Length % 4) { 2 { $s += '==' } 3 { $s += '=' } }
    [Convert]::FromBase64String($s)
}

[Text.Encoding]::UTF8.GetString((ConvertFrom-Base64Url 'eyJhbGciOiJIUzI1NiJ9'))
# {"alg":"HS256"}
Check your output

Not sure the decode worked? Paste the original string into the decoder. It shows the text, the hex bytes and the detected file type side by side, so you can compare it with what your code produced. Nothing is uploaded; it runs in your browser.


Published 9 November 2026. Spot an error? Email contactus@base64decode.tools.

Frequently asked

Common questions.

Use [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($s)) for text. For binary data, pass the byte array from FromBase64String to [IO.File]::WriteAllBytes with a full output path.

-EncodedCommand payloads are UTF-16LE. Decode them with [Text.Encoding]::Unicode.GetString instead of UTF8. Decoding UTF-16 as UTF-8 leaves a null character between every letter.

Decoding is safe because it only converts text. Running the result is not. Never pipe it into Invoke-Expression; save it to a file and read it, ideally on an isolated machine.

.NET uses the process working directory for relative paths, which may differ from PowerShell's current location. Use a full path, for example "$PWD\output.pdf".

Not directly. Replace - with + and _ with /, add = padding until the length is a multiple of 4, then call [Convert]::FromBase64String.

Keep going

Related guides and tools.