DEV Community

Cover image for Decoding Base64 With PowerShell
Mathabo Motaung
Mathabo Motaung

Posted on

Decoding Base64 With PowerShell

Using Windows PowerShell to decode base64

Developers often encounter Base64 strings. You might see them in configuration files or API responses. They represent binary data in a text format.

Comparison

Linux users decode these strings easily. They use the terminal command base64 with the decode flag. It is quick and native.

Windows users often struggle with this task. PowerShell does not have a direct equivalent to the Linux command. You might try to find a specific cmdlet. You will not find one.

Fact

PowerShell relies on the .NET framework. This is a strength. It gives you access to powerful system libraries. You can use these libraries to manipulate data.

Knowledge

Here is the logic. Base64 is a string. You must convert this string into a byte array. Then you convert that byte array into readable text.

Follow these steps to decode a string:

Step 1: Define your Base64 string. Assign it to a variable.

Step 2: Convert the string to bytes. Use the System.Convert class. It has a static method called FromBase64String.

$Bytes = [System.Convert]::FromBase64String($EncodedString)
Enter fullscreen mode Exit fullscreen mode

Step 3: Decode the bytes. Use the System.Text.Encoding class. The UTF8 property works for most modern text. Use the GetString method on the byte array.

The output will be the decoded text. In this example, it is Hello World.

Summary

This method is reliable. It works on all modern versions of PowerShell. It does not require external tools. It scripts well for automation tasks.

Understanding the underlying types helps you. You are not just running a black box command. You are manipulating data types directly. This is the PowerShell way.

Top comments (0)