How to Convert Base64 String to Byte Array in PowerShell?

Today, I was checking how to convert a Base64 string to a byte array in PowerShell. I tried various methods. In this tutorial, I will show you how to convert a Base64 string to a byte array in PowerShell with examples.

To convert a Base64 string to a byte array in PowerShell, you can use the System.Convert class’s FromBase64String method. For example, given a Base64 string $base64String = "SGVsbG8gV29ybGQh", you can convert it by running $byteArray = [System.Convert]::FromBase64String($base64String). This method decodes the Base64 string into its equivalent byte array in PowerShell.

What is Base64 Encoding in PowerShell?

Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It is commonly used to encode data for transmission over media that are designed to handle text. This encoding helps to ensure that the data remains intact without modification during transport.

Convert Base64 String to Byte Array in PowerShell

Now, let me show you how to convert Base64 string to byte array in PowerShell using different methods.

Method 1: Using System.Convert() Class

The most straightforward way to convert a Base64 string to a byte array in PowerShell is by using the System.Convert class, which provides built-in methods for such conversions.

Example

Here’s a simple example to understand this better:

# Base64 encoded string
$base64String = "SGVsbG8gV29ybGQh"

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

# Output the byte array
$byteArray

In this example, the Base64 string "SGVsbG8gV29ybGQh" (which represents “Hello World!”) is converted into a byte array using the FromBase64String method of the System.Convert class. This method decodes the Base64 string into its equivalent 8-bit unsigned integer array.

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

Convert Base64 String to Byte Array in PowerShell

Read How to Convert String to Double in PowerShell?

Method 2: Using Custom Function

You can also create a custom function in PowerShell to convert Base64 String to Byte Array in PowerShell.

Here is an example:

Example

function Convert-Base64ToByteArray {
    param (
        [string]$Base64String
    )

    return [System.Convert]::FromBase64String($Base64String)
}

# Base64 encoded string
$base64String = "SGVsbG8gV29ybGQh"

# Convert Base64 string to byte array using the custom function
$byteArray = Convert-Base64ToByteArray -Base64String $base64String

# Output the byte array
$byteArray

This custom function, Convert-Base64ToByteArray, takes a Base64 string as input and returns the corresponding byte array. This approach makes the script more modular and easier to maintain.

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

PowerShell Convert Base64 String to Byte Array

Check out Convert String to Hashtable in PowerShell

Method 3: Using System.Text.Encoding

For those who need to handle different text encodings, the System.Text.Encoding class can be used in conjunction with Base64 conversions. This method is more advanced and useful when dealing with various character encodings.

Example

Here is an example.

# Base64 encoded string
$base64String = "SGVsbG8gV29ybGQh"

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

# Convert byte array to string (UTF8 encoding)
$utf8String = [System.Text.Encoding]::UTF8.GetString($byteArray)

# Output the UTF8 string
$utf8String

In this example, after converting the Base64 string to a byte array, the byte array is further converted to a UTF8 string using the System.Text.Encoding class. This can be particularly useful when dealing with text data that needs further re-encoded or processed.

Conclusion

In this tutorial, I have explained how to Convert a Base64 string to a byte array in PowerShell using various methods.

  • Using System.Convert() Class
  • Using Custom Function
  • Using System.Text.Encoding

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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