Skip to content

Base64 Encoding with PowerShell

Base64 encoding is a method used to encode binary data into an ASCII string format by translating it into a radix-64 representation. It is commonly used in various applications, including encoding email attachments, JSON web tokens, and data storage. This method ensures that the data remains intact without modification during transport.

PowerShell includes built-in support for base64 encoding and decoding. In this guide, we’ll explore how to perform base64 encoding in PowerShell.

What is Base64 Encoding?

Base64 encoding converts binary data into a text format using 64 different ASCII characters. These characters include uppercase letters (A-Z), lowercase letters (a-z), numbers (0-9), and two additional symbols (+ and /). This encoding is particularly useful for encoding data that needs to be safely transmitted over media that are designed to deal with textual data.

PowerShell and Base64 Encoding

PowerShell provides several ways to handle base64 encoding. The most straightforward method involves using the [Convert]::ToBase64String() method for encoding and [Convert]::FromBase64String() for decoding.

Encoding a string to Base64

Here is a PowerShell script to encode the string “” to base64:

# Define the string to encode
$plainText = ""

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

# Encode the byte array to base64
$base64Encoded = [Convert]::ToBase64String($bytes)

# Display the base64 encoded string
Write-Output $base64Encoded




PowerShell, with its robust scripting capabilities, provides straightforward methods for performing base64 encoding and decoding.

By utilising the [System.Text.Encoding]::UTF8.GetBytes() method to convert a string into a byte array and [Convert]::ToBase64String() to encode it, PowerShell allows for efficient and effective data encoding. This process is particularly useful in scenarios such as data serialisation, web development, and secure data transfer.

Recommended Reading: Base64 Decoding with PowerShell

Published inPowerShell
© 2024 - Powered by Coffee & Magic