Tag Archives: PowerShell Programming language

PowerShell is a task automation and configuration management program from Microsoft, consisting of a command-line shell and the associated scripting language. Platforms are Windows PowerShell .NET Framework, Implementation language on C#.

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.

How to Ping Multiple Devices at Once in PowerShell?

Ping a Range of IP Addresses using Windows PowerShell

Ping an IP range in the PowerShell, you can instantly determine if the IP addresses within a subnet are available and reachable on a windows machine. It also shows you which IPs are connected to a device und which device is currently online.

Launch start or press the Windowskey, then type powershell on the keyboard, select powershell app in the chooser.

Out from the opened PowerShell run the two lines one after each other with use copy and paste.

$ping = New-Object System.Net.Networkinformation.Ping
1..10 | % { $ping.send("192.168.10.$_") | where {$_.status -eq "Success"} | Select-Object Address,Status }

  IP range “1..10” Change the start (1) or end (10) to the IP range you want to ping on the network. Note that the last octet figure can’t exceed 255.

The command sends Ping to an IP range as ICMP echo request messages in the PowerShell. To all IPv4 addresses in this range and wait for echo replies. The output will look similar this.

If you get truncated output then use the command bellow.

10..100 | % { $ping.send("192.168.10.$_") | where {$_.status -eq "Success"} | Select-Object -Property Address,Status | Format-Table -AutoSize }

Remarks Ping Range in PowerShell

This PowerShell class provides functionality similar to the Ping.exe command line tool. The Ping class sends an Internet Control Message Protocol (ICMP) echo request message to a remote computer and wait for an ICMP echo reply message from that computer.

Conclusion

This post shows how you can easily ping scan IP subnet in PowerShell. Without using additional tools that have to be installed first.