PowerShell Automation Guide
PowerShell Base64 Decode & Encode: The Simple Guide
Stop guessing. Here are the exact, ready-to-use commands for handling Base64 in your PowerShell scripts.
The Core Idea
PowerShell uses .NET to handle Base64. The process always involves converting your data to a "byte array" as a middle step.
Task 1: Decode a Base64 String
This is what you'll do most often. You have a piece of Base64 text and you need to see what it says.
Step-by-Step Code
# Your Base64 string goes here
$base64Input = "SGVsbG8sIFBvd2VyU2hlbGwh"
# Step 1: Convert the Base64 string into bytes
$bytes = [System.Convert]::FromBase64String($base64Input)
# Step 2: Convert the bytes back to a readable string (using UTF-8)
$originalText = [System.Text.Encoding]::UTF8.GetString($bytes)
# Display the result
Write-Host $originalText
The Result
Task 2: Encode a String to Base64
Here, you'll take regular text and convert it into a Base64 string, often to safely transmit it or include it in a script.
Step-by-Step Code
# The text you want to encode
$plainText = "Hello, PowerShell!"
# Step 1: Convert your text into bytes
$bytes = [System.Text.Encoding]::UTF8.GetBytes($plainText)
# Step 2: Convert those bytes into a Base64 string
$base64Output = [System.Convert]::ToBase64String($bytes)
# Display the result
Write-Host $base64Output
The Result
Need a quick check? Paste your string into our Online Base64 Tool to instantly verify your script's output.
Task 3: Work with Files (Encode & Decode)
The same logic applies to files. You just need to read the file into bytes first, or write the decoded bytes back to a new file.
Example A: Encode a File into a Base64 String
$inputFile = "C:\Users\alex\Documents\secret.txt"
$outputFile = "C:\Users\alex\Documents\secret.b64.txt"
# Read the entire file as bytes
$fileBytes = [System.IO.File]::ReadAllBytes($inputFile)
# Convert the file bytes to a Base64 string
$base64String = [System.Convert]::ToBase64String($fileBytes)
# Save the Base64 string to a new file
$base64String | Set-Content -Path $outputFile
Write-Host "File encoded successfully!"
Example B: Decode a Base64 String back into a File
$inputFile = "C:\Users\alex\Documents\secret.b64.txt"
$outputFile = "C:\Users\alex\Documents\secret_restored.txt"
# Read the Base64 string from the input file
$base64String = Get-Content -Path $inputFile -Raw
# Convert the Base64 string back to bytes
$fileBytes = [System.Convert]::FromBase64String($base64String)
# Write those bytes to a new file
[System.IO.File]::WriteAllBytes($outputFile, $fileBytes)
Write-Host "File decoded successfully!"
Common Questions (FAQ)
Why does my decoded text look like random symbols?
This usually means the Base64 string did not represent text. For example, it could be a ZIP file, an image, or a PDF. When you decode it to bytes and try to display it as text, it appears as gibberish. If you expect a file, make sure to use the "Decode to File" method.
Is there a simpler command like `ConvertTo-Base64`?
Not built-in, no. While you could create your own PowerShell function to wrap these .NET calls, the methods shown here are the standard, officially supported way to perform Base64 operations in PowerShell and are extremely reliable.
What's the `[System.Text.Encoding]::UTF8` part?
That tells PowerShell how to interpret the bytes as text characters. UTF-8 is the universal standard for web and modern systems. If you were working with very old Windows files, you might occasionally need `::ASCII` or `::Unicode`, but 99% of the time, `UTF8` is the correct choice.