Developer Guide

PowerShell Base64 Decode & Encode

How to Base64 decode and encode strings, files, and credentials in PowerShell using System.Convert — with practical examples for API calls, secrets, and automation.

Last updated: May 2026

How Do You Base64 Encode a String in PowerShell?

Use [System.Convert]::ToBase64String() combined with [System.Text.Encoding]::UTF8.GetBytes() to encode any string to Base64. PowerShell has full access to the .NET framework, so the same System.Convert class used in C# is available directly.

# Encode a string to Base64
$text = "Hello World"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($text)
$encoded = [System.Convert]::ToBase64String($bytes)
Write-Output $encoded
# Output: SGVsbG8gV29ybGQ=

Always specify [System.Text.Encoding]::UTF8 explicitly rather than relying on the system default encoding. The default encoding varies between Windows versions and locales, which can produce inconsistent output across machines. The result uses the standard Base64 alphabet (A-Z, a-z, 0-9, +, /) with = padding as defined in RFC 4648. For a browser-based alternative, use the Base64 text encoder.

How Do You Decode Base64 in PowerShell?

Use [System.Convert]::FromBase64String() to convert a Base64 string back to a byte array, then [System.Text.Encoding]::UTF8.GetString() to reconstruct the original text. The FromBase64String() method validates the input and throws System.FormatException if the string contains invalid characters or malformed padding.

# Decode a Base64 string
$encoded = "SGVsbG8gV29ybGQ="
$bytes = [System.Convert]::FromBase64String($encoded)
$decoded = [System.Text.Encoding]::UTF8.GetString($bytes)
Write-Output $decoded
# Output: Hello World

Wrap decoding in a try/catch block in production scripts to handle malformed input gracefully. Invalid Base64 input — such as strings with incorrect padding or characters outside the Base64 alphabet — causes an exception rather than silently returning garbage data. To decode Base64 directly in your browser, use the Base64 text decoder.

How Do You Encode a File to Base64 in PowerShell?

Use [Convert]::ToBase64String() with [IO.File]::ReadAllBytes() to encode a binary file to Base64 in a single line. This loads the entire file into memory, so it works best for files that fit within available RAM. For large files, use a stream-based approach.

# Encode a file to Base64
$filePath = "C:path	oile.png"
$fileBytes = [IO.File]::ReadAllBytes($filePath)
$encoded = [Convert]::ToBase64String($fileBytes)
Write-Output $encoded

# Save the Base64 output to a text file
$encoded | Out-File -FilePath "C:path	oile.b64" -NoNewline -Encoding ascii

# Create a data URI for HTML embedding
$dataUri = "data:image/png;base64," + $encoded
Write-Output $dataUri

The -NoNewline flag on Out-File prevents PowerShell from appending a trailing newline, which would corrupt the Base64 string. Specify -Encoding ascii to avoid the UTF-16 BOM that PowerShell writes by default. The Base64 file encoder performs this same operation in your browser without uploading the file to any server.

How Do You Use Base64 for Basic Auth in PowerShell?

HTTP Basic Authentication encodes credentials as username:password in Base64 and sends them in the Authorization header. PowerShell's Invoke-RestMethod and Invoke-WebRequest cmdlets accept custom headers, so you can construct a Basic Auth header manually using [Convert]::ToBase64String().

# Build a Basic Auth header
$credentials = "user:password"
$bytes = [Text.Encoding]::UTF8.GetBytes($credentials)
$b64 = [Convert]::ToBase64String($bytes)
$headers = @{ Authorization = "Basic $b64" }

# Use with Invoke-RestMethod
$response = Invoke-RestMethod -Uri "https://api.example.com/data" -Headers $headers -Method Get
Write-Output $response

# One-liner version
$response = Invoke-RestMethod -Uri "https://api.example.com/data" -Method Get \    -Headers @{ Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("user:password")) }

Never hardcode credentials in scripts that are committed to version control. Use Get-Credential to prompt for credentials at runtime, or store secrets in environment variables or a secrets manager. PowerShell 7+ supports -Credential on many cmdlets which handles the encoding automatically. For understanding the encoding format, see the What is Base64 guide.

PowerShell vs Terminal Base64 Commands

Base64 encoding commands differ significantly across platforms. The table below compares the syntax for encoding a string on PowerShell (Windows), Linux bash, and macOS Terminal, which helps when writing cross-platform scripts or documentation.

PlatformEncode StringDecode String
PowerShell [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("text")) [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("dGV4dA=="))
Linux (bash) echo -n "text" | base64 echo "dGV4dA==" | base64 -d
macOS Terminal echo -n "text" | base64 echo "dGV4dA==" | base64 -D
PowerShell Core (7+) Same .NET syntax as Windows PowerShell — works cross-platform

Note that Linux uses -d and macOS uses -D for decoding — a common source of script portability issues. PowerShell Core (version 7+) runs identically on Windows, Linux, and macOS, making it a reliable choice for cross-platform automation. For Linux-specific commands, see the Base64 in Linux guide. For macOS, see the Base64 on macOS guide.

Frequently Asked Questions

How do I Base64 encode in PowerShell?

Use [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("your text")). This converts the string to UTF-8 bytes first, then encodes those bytes as a Base64 string using the standard RFC 4648 alphabet. No additional modules or cmdlets are required — the System.Convert class is part of the .NET framework included with all PowerShell versions.

How do I decode Base64 in PowerShell?

Use [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("your_base64")). The FromBase64String() method decodes the Base64 string to a byte array, and GetString() converts those bytes back to a readable string using UTF-8 encoding. If the input contains invalid Base64 characters, a System.FormatException is thrown.

How do I use Base64 for Basic Auth in PowerShell?

Encode "username:password" as Base64 using [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("user:pass")), then pass it in an Authorization header as "Basic " + $b64 when calling Invoke-RestMethod or Invoke-WebRequest. This constructs the HTTP Basic Authentication header defined in RFC 7617.

Can I use Base64 encoding in PowerShell scripts without external tools?

Yes. PowerShell includes full access to the .NET framework, which provides System.Convert.ToBase64String() and System.Convert.FromBase64String(). No external modules, packages, or third-party tools are needed. These methods are available in Windows PowerShell 2.0 and later, as well as all versions of PowerShell Core (6+) and PowerShell 7+.

Related Base64 Tools and Guides