Encoding image files into a base64 string using Powershell

I was recently looking at encoding image files into a Base64 string to place directly into HTML and CSS, whilst there is a number of useful online tools to complete this task, I decided to have a look at performing this task using Windows Powershell.

This can be achieved by using the [convert]::ToBase64String method and is quite a simple process to retrieve the Base64 string, in  fact a one liner.

By invoking the method agaisnt the content of a file using the Get-Content cmdlet to encodes a set of characters into a sequence of bytes, we retrieve the information we require.

In the below example I am retrieving the Base64 string for the image file D:\Images\image1.jpg

[convert]::ToBase64String((Get-Content D:\Images\image1.jpg -Encoding byte))

 

Advertisements

One thought on “Encoding image files into a base64 string using Powershell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s