Decoding Base64 With PowerShell
Source: Dev.to
Introduction
Developers often encounter Base64 strings in configuration files or API responses. They represent binary data in a text format. While Linux users can decode these strings easily with the base64 command, Windows users typically lack a direct equivalent in PowerShell.
PowerShell relies on the .NET framework, which provides powerful system libraries for data manipulation. By using these libraries, you can decode Base64 strings without external tools.
How Base64 Decoding Works
-
Base64 string → byte array
Convert the Base64‑encoded string into a byte array. -
Byte array → readable text
Decode the byte array into a string using an appropriate text encoding (UTF‑8 works for most modern text).
Step‑by‑Step Guide
Step 1: Define the Base64 string
$EncodedString = "SGVsbG8gV29ybGQ=" # Example: "Hello World"
Step 2: Convert the string to a byte array
$Bytes = [System.Convert]::FromBase64String($EncodedString)
Step 3: Decode the byte array to text
$DecodedText = [System.Text.Encoding]::UTF8.GetString($Bytes)
Result
Hello World
Benefits
- Works on all modern versions of PowerShell.
- No external tools required.
- Ideal for automation scripts.
Understanding the underlying .NET types lets you manipulate data directly, embodying the PowerShell way of working with objects rather than treating commands as black boxes.