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))
Found your line of code, dropped it into my script, and it just worked. Doesn’t often happen. Thanks!
LikeLiked by 1 person