How to Convert String to Base64 in PowerShell?

Recently, my client asked us for a requirement to convert string to base64. We used PowerShell for this. In this tutorial, I will show you how to convert string to base64 in PowerShell using different methods with examples.

To convert a string to Base64 in PowerShell, you can use the [System.Convert] class. First, convert the string to a byte array using [System.Text.Encoding]::UTF8.GetBytes($string), then encode it to Base64 with [System.Convert]::ToBase64String($bytes). For example:

$string = "Hello, World!"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($string)
$base64String = [System.Convert]::ToBase64String($bytes)
$base64String

This will produce the Base64 encoded string of the original text.

What is Base64 Encoding?

Base64 encoding is a method for converting binary data into an ASCII string format by translating it into a radix-64 representation. This is particularly useful for encoding data that needs to be stored and transferred over media designed to handle text.

Convert String to Base64 in PowerShell

Now. let me show you different methods to convert string to Base64 in Powershell with examples.

Method 1: Using [System.Convert] Class

The simplest method to convert a string to Base64 in PowerShell is by using the [System.Convert] class. Here’s how you can do it:

Example

Here is an example.

# Original string
$string = "Hello, New York!"

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

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

# Output the Base64 string
$base64String

In this example:

  1. We start with the string "Hello, New York!".
  2. We convert the string into a byte array using UTF-8 encoding.
  3. We then convert the byte array into a Base64 string using [System.Convert]::ToBase64String.

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

Convert String to Base64 in PowerShell

Check out Convert String to Hashtable in PowerShell

Method 2: Using [System.Text.Encoding] Class

Another method to convert string to Base64 is by using the [System.Text.Encoding] class directly to handle the encoding and conversion process.

Example

Here is a complete example of how to do it.

# Original string
$string = "PowerShell Base64 Encoding"

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

# Encode byte array to Base64 string
$base64String = [Convert]::ToBase64String($bytes)

# Output the Base64 string
$base64String

This method is similar to the first one but emphasizes the use of System.Text.Encoding to handle the string-to-byte conversion explicitly.

You will get the exact output in the screenshot below after you execute the above PowerShell script.

PowerShell Convert String to Base64

Method 3: Using Custom Function

In PowerShell, you can also create a custom function to reuse the logic and handle multiple encodings. Here is an example of how to do it.

Example

function ConvertTo-Base64 {
    param (
        [string]$inputString
    )

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

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

    return $base64String
}

# Use the custom function
$encodedString = ConvertTo-Base64 -inputString "Custom Function Example"
$encodedString

In this example, the ConvertTo-Base64 function takes a string as input, converts it to a byte array, and then encodes it into a Base64 string. This function can be reused throughout your scripts for consistent Base64 encoding.

Check out Convert String to JSON in PowerShell

Decode Base64 Strings in PowerShell

To decode a Base64 string back to its original form, you can use the [System.Convert] class again, but this time with the FromBase64String method.

Example

Here is a complete example.

# Base64 encoded string
$base64String = "SGVsbG8sIFdvcmxkIQ=="

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

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

# Output the original string
$originalString

This example demonstrates how to decode a Base64 string back to its original text by reversing the encoding process.

You can see the screenshot below, I executed the above PowerShell script using VS code.

Decode Base64 Strings in PowerShell

Conclusion

As a PowerShell developer, you will get this requirement very often, and that is how to convert strings to Base64 in PowerShell. I have explained how to do it using different methods, like using the [System.Convert] class and the [System.Text.Encoding] class, and also you can create a custom function to reuse it. I have also discussed how to decode base64 strings in PowerShell.

Still, do you have some questions? Feel free to leave a comment below. I will try to reply as soon as possible.

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.