All posts by Don Matteo

lebt in der Schweiz, ist System Engineer MCP bei A-Enterprise GmbH. Mitglied des UNBLOG Knowledge Network. Author und Blogger zu den Themen, Tutorials für Linux und Open Source.

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

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

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.

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.

mkdir multiple directories at once

The ability to create multiple directories using mkdir at once is always useful when you need numbered directories, for example when working with archives or for testing purposes. However, creating the directories manually would be very boring and time-consuming if there were a large number of directories.

for i in {1..10}
        do mkdir “$i”
done

But thanks to the versatile options in the command line, this task can be solved quickly and easily using for loops.

mkdir multiple directories at once in Linux

Here I’ll show you usefull examples how to create multiple directories at once using mkdir here in the Linux bash with just one command.

$ for i in {1..10}; do mkdir dir"$i" ;done

The number of sequenses between the curly braces can be increased as desired, for example start at 1 and go to 100.

Listing the current directory shows the following output after mkdir.

user@linux:~$ ls -al
total 314452
drwxr-xr-x 18 user user      4096 Jan  7 16:46 .
drwxr-xr-x  4 user user      4096 Oct 11 15:56 ..
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir1
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir10
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir2
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir3
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir4
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir5
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir6
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir7
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir8
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir9

In Linux bash and zsh it’s even easier, as the mkdir sequenses shown.

$ mkdir dir{1..10}

If there should now be a file in each directory, you can do this with the next command.

$ for i in {1..10}; do echo -e "This is the message" > dir"$i"/readme.txt ;done

This command create a file in multiple directories at once using for loop .

If you want to set the owners and permission to multiple directories at once then this command is used.

$ for i in {1..10}; do chown -R user:user dir"$i" ;done
$ for i in {1..10}; do chmod -R 755 dir"$i" ;done

If you do not need the directories anymore and can to be deleted again, this command can do it just as quickly at once in the Linux shell.

$ for i in {1..10}; do rm -rf dir"$i" ;done

Create multiple directories in Windows at once

In Windows you can open notepad and insert the following line and save it as a batch file (.bat). Then open a command prompt and simply run the batch file.

@echo off
for /L %%n in (1,1,10) do mkdir c:\temp\dir%%n

Alternatively, this line also works and create multiple directories at once using mkdir in command prompt.

for /L %%n in (1,1,10) do md dir%%n

This command create multiple directories at once using in batch file.

Create multiple directories using PowerShell

In Windows PowerShell this can be done with this command.

for ($i=1;$i -le 10;$i++){MD "dir$i"}

This PowerShell commnad create multiple directories at once in a loop.

With a less neasty PowerShell command can do the same.

1..10 | ForEach-Object { New-Item -ItemType Directory -Path "C:\Temp\dir$_"}

This PowerShell command create multiple directories at once using the ForEach-Object.