Convert Text to Base64 and back in PowerShell

()

Base64 allows to convert binary data to text and text to binary data, here in PowerShell. Base64 encoding is a process of converting binary data into an ASCII string format.

The Base64 format converts data (mainly images) into characters, originally with a set of 64 characters (name origin), which can be found in any ISO 8859-1 (also known as Latin-1) character set and can therefore be displayed anywhere. Or to put it another way: An image becomes HTML code, because Base64 has now become the HTML standard and is therefore used for Internet objects, especially for inline graphics.

Further more Base64 encoding is also commonly used across mail systems to send email attachments, as SMTP in its original form was only designed to transport 7-bit ASCII characters. Encoding an attachment as Base64 before sending and then decoding it upon receipt ensures that older SMTP servers do not interfere with the attachment.

Convert Text to Base64 in PowerShell

The following function Set-Base64 allows to convert a text to Base64 in PowerShell. The Get-Base64 function ensures conversion back to human readable text.

Function Set-Base64
{
	param (
	[String]$arg
	)
	$textout = [System.Text.Encoding]::UTF8.GetBytes($arg)
	[Convert]::ToBase64String($textout)
}

Function Get-Base64
{
	param (
	[String]$arg
	)
	$textout = [Convert]::FromBase64String($arg)
	[System.Text.Encoding]::UTF8.GetString($textout)
}

Note. If you add these functions under your %userprofile%\Documents\WindowsPowerShell path to the file Microsoft.PowerShell_profile.ps1, the two commands will be available for execution in your PowerShell.

Convert Base64 Code to Text

In PowerShell, Set-Base64 converts the text to Base64 encoding, Get-Base64 decoding back to human readable text.

PS C:\> Set-Base64 "this is a text converted to base64 encoding"
dGhpcyBpcyBhIHRleHQgY29udmVydGVkIHRvIGJhc2U2NCBlbmNvZGluZw==

PS C:\> Get-Base64 dGhpcyBpcyBhIHRleHQgY29udmVydGVkIHRvIGJhc2U2NCBlbmNvZGluZw==
this is a text converted to base64 encoding

The Base64 encoding and decoding in PowerShell then looks like this.

Convert Text to Base64 and back in PowerShell
Convert Text to Base64 and back in PowerShell

Convert binary file to Base64 in PowerShell

Files and images can be converted to Base64 text using the PowerShell cmdlet Get-Content with the -Encoding Byte and -Raw parameters.

[convert]::ToBase64String((get-content -Path C:\temp\image.png -encoding byte -Raw))

The -Raw parameter ensures that the file is read in its entirety and not line by line. -Encoding Byte tells PowerShell that it is a binary file.

PowerShell: ToBase64String, Convert file to Base64 in PowerShell
PowerShell: ToBase64String

Image conversion to Base64 Text locally

Convert image.png to Base64, use the PowerShell convert command with parameters, the Base64 encoded text is written to the image.txt file.

[convert]::ToBase64String((get-content -Path C:\temp\image.png -encoding byte -Raw)) > C:\temp\image.txt
Convert Image to Base64 Text locally

How do you know Base64 Encoding is doing?

Let’s take another example with the image file bluebox.png these you can download to your computer, just convert the small 10×10 pixel graphic to Base64 encoding using this command line in your PowerShell.

[convert]::ToBase64String((get-content -Path C:\temp\bluebox.png -encoding byte -Raw)) > C:\temp\img.txt

Next, create an html file with the content as shown below,

<div>
  <p>A blue box</p>
  <img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYSURBVChTY2RQ+8+AGzBBaRxgZEozMAAAbOYBOV4WE6YAAAAASUVORK5CYII=" alt="Blue box" />
</div>

Insert the content from the file C:\temp\img.txt, place the Base64 between <img src="data:image/png;base64,code here" alt="Bluebox"/>

Double-click the html file you just created. If you’ve done everything right, a small blue box will shown in your browser.

How do you know Base64 encoding is doing?

Addendum

Base64 is not an encryption method. It is simply an encoding method used to convert binary data into a human-readable form. It does not provide security or privacy of data.

Base64 encodes binary data by converting it into a string of ASCII characters. This is useful when transferring binary data to environments that only support text data, such as email systems.

  Sending e-mail with attachment using “content-transfer-encoding: base64” more about in this post.

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *