How to Convert Base64 String to Text in PowerShell?

Recently, someone in the PowerShell development team asked me to convert the Base64 string to text in Powershell. I explained how to do this using various methods. In this tutorial, I will show you how to convert Base64 string to text in PowerShell with examples.

To convert a Base64 string to text in PowerShell, you can use the [System.Convert]::FromBase64String() method. This method decodes the Base64 string into a byte array, which can then be converted to a readable text string using [System.Text.Encoding]::UTF8.GetString(). For instance, given a Base64 string $base64String = "SGVsbG8gV29ybGQh", you can decode it with:

$bytes = [System.Convert]::FromBase64String($base64String)
$text = [System.Text.Encoding]::UTF8.GetString($bytes)
Write-Output $text

This will output “Hello World!”.

What Are Base64 Encoding in PowerShell?

Base64 encoding is a method for encoding binary data into an ASCII string format by translating it into a radix-64 representation. Each Base64 digit represents exactly 6 bits of data. This encoding is commonly used in various applications, including email via MIME, storing complex data in XML or JSON, and embedding image data within HTML or CSS.

Convert Base64 String to Text in PowerShell

Now, let me show you how to convert Base64 string to text in PowerShell using various methods with examples.

Method 1: Using [System.Convert]::FromBase64String()

The best method to decode a Base64 string in PowerShell is by using the [System.Convert]::FromBase64String() method. This method converts the Base64 encoded string back to a byte array, which can then be converted to a readable text string.

Here is an example.

# Base64 encoded string
$base64String = "SGVsbG8gV29ybGQh"

# Decode Base64 string to byte array
$bytes = [System.Convert]::FromBase64String($base64String)

# Convert byte array to text
$text = [System.Text.Encoding]::UTF8.GetString($bytes)

# Output the text
Write-Output $text

In this example, the Base64 string SGVsbG8gV29ybGQh is decoded to Hello World!.

You can see the output in the screenshot below after I executed the above PowerShell script using VS code.

Convert Base64 String to Text in PowerShell

Check How to Convert Files to Base64 in PowerShell?

Method 2: Using Base64 Module

Now, let me show you another method: using the Base64 module, which provides a more streamlined approach for encoding and decoding Base64 strings.

First, you need to install the module if you haven’t already:

Install-Module -Name Base64

Here is a complete example to understand it better.

# Import the Base64 module
Import-Module Base64

# Base64 encoded string
$base64String = "SGVsbG8gV29ybGQh"

# Decode Base64 string
$text = $base64String | ConvertFrom-Base64

# Output the text
Write-Output $text

Check out How to Convert String to JSON in PowerShell?

Method 3: Using Custom Function

You can also create your own function to decode Base64 strings in PowerShell. This can be useful for adding additional functionality or custom logging.

Here is an example.

function Decode-Base64String {
    param (
        [string]$base64String
    )

    # Decode Base64 string to byte array
    $bytes = [System.Convert]::FromBase64String($base64String)

    # Convert byte array to text
    $text = [System.Text.Encoding]::UTF8.GetString($bytes)

    return $text
}

# Base64 encoded string
$base64String = "SGVsbG8gV29ybGQh"

# Decode using custom function
$text = Decode-Base64String -base64String $base64String

# Output the text
Write-Output $text

I executed the above script, and you can see the output in the screenshot below:

PowerShell Convert Base64 String to Text

Conclusion

In this tutorial, I will show you how to convert a Base64 string to text in PowerShell using different methods, such as using [System. Convert]:: FromBase64String (), using the Base64 Module, etc. I have also explained how to create a custom function to convert a Base64 String to Text in PowerShell.

You may like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.