How to Convert Bytes to Base64 String in PowerShell?

Base64 encoding is a common technique used to represent binary data in an ASCII string format. This is particularly useful when you need to encode binary data for text-based protocols such as email or JSON. In PowerShell, converting bytes to a Base64 string can be done efficiently using built-in .NET methods. In this tutorial, I will show you how to convert bytes to base64 string in PowerShell using different methods.

To convert bytes to a Base64 string in PowerShell, you can use the System.Convert class. First, create a byte array and then use [System.Convert]::ToBase64String($byteArray) to encode it. For example, $byteArray = [byte[]](1, 2, 3, 4, 5); $base64String = [System.Convert]::ToBase64String($byteArray); Write-Output $base64String.

Convert Bytes to Base64 String in PowerShell

Now, let me show you how to convert bytes of Base64 string in PowerShell using the below methods.

Method 1: Using System.Convert Class

The best way to convert a byte array to a Base64 string in PowerShell is by using the System.Convert class. This method is simple and leverages the powerful .NET framework. Let me show you an example.

Example:

# Sample byte array
$byteArray = [byte[]](1, 2, 3, 4, 5)

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

# Output the Base64 string
Write-Output $base64String

In this example, we first create a sample byte array. Then, we use the ToBase64String method of the System.Convert class to encode the byte array into a Base64 string. Finally, we output the Base64 string.

Here is the output in the screenshot below, after I executed the above PowerShell script using VS code.

Convert Bytes to Base64 String in PowerShell

Check out Convert String to Double in PowerShell

Method 2: Reading a File and Encoding to Base64

Sometimes, you might need to read the contents of a file and convert it to a Base64 string. This can be done by reading the file into a byte array and then using the System.Convert class.

Here is an example to understand it better.

Example:

# Path to the file
$filePath = "C:\MyFolder\file.txt"

# Read the file into a byte array
$byteArray = [System.IO.File]::ReadAllBytes($filePath)

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

# Output the Base64 string
Write-Output $base64String

In this example, we read the contents of a file into a byte array using the ReadAllBytes method of the System.IO.File class. Then, we convert the byte array to a Base64 string using the ToBase64String method.

Read How to Convert String to Hashtable in PowerShell?

Method 3: Encode a String to Base64

If you have a string that you want to encode to Base64, you first need to convert the string to a byte array. This can be done using the GetBytes method of the System.Text.Encoding class.

Let me show you an example of how this works.

Example:

# Sample string
$string = "Hello, World!"

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

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

# Output the Base64 string
Write-Output $base64String

Here, we convert a sample string to a byte array using UTF-8 encoding. Then, we encode the byte array to a Base64 string using the ToBase64String method.

Here is the output in the screenshot below after I executed the above PowerShell script.

PowerShell Convert Bytes to Base64 String

Check out How to Convert String to JSON in PowerShell?

Method 4: Using ConvertTo-Base64 Cmdlet

For those using PowerShell Core (7.0 and later), there’s a built-in cmdlet ConvertTo-Base64 that simplifies this process.

Here is an example.

Example:

# Sample byte array
$byteArray = [byte[]](1, 2, 3, 4, 5)

# Convert byte array to Base64 string using ConvertTo-Base64
$base64String = $byteArray | ConvertTo-Base64

# Output the Base64 string
Write-Output $base64String

In this example, we use the ConvertTo-Base64 cmdlet to directly encode a byte array to a Base64 string.

Conclusion

In this tutorial, I have explained how to convert Bytes to Base64 Strings in PowerShell using different methods. However, I recommend using the Using System.Convert Class method. I hope this helps.

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.