Home » PowerShell Tips » PowerShell – Base64 Encoding

PowerShell – Base64 Encoding

In programming languages, Base64 encoding represents binary data in ASCII string format by translating data into Radix 64 representation. Base64 encoding is a group of binary to text encoding schemes.

Base 64 encoding is used when we want to transfer data over media or network in ASCII string format so that data remain intact on transport.

In this article, I will explain you how to use base64 encoding in PowerShell for image, command, zip file, utf8.

Let’s understand base64 encoding in PowerShell with examples.

PowerShell Base64 Encode File Content

Use PowerShell script to take a file as input, read the contents of the file using Get-Content cmdlet.

PowerShell base64 encode the file contents and save file to location with .64 file format.

$FileName = "D:\Logs-FTP01\EncodeLogic.txt"

# Get content of the file
$FileContent = Get-Content $FileName

#Get the bytes of the file content with encode
$fileContentInBytes = [System.Text.Encoding]::UTF8.GetBytes($FileContent)

# Base64 Encode file 
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentInBytes)

# Save Base64 Enconde file with extension .b64
$fileContentEncoded | Set-content ($fileName + ".b64")

Write-Host $FileName + ".b64" + "File Encoded Successfully!"

In the above PowerShell script to base64 encode file,

$FileName – contains file path

$FileContent – contains content of file using Get-Content cmdlet

$fileContentInBytes – contains bytes about file content, get it using UTF8.GetBytes() method

$fileContentEncoded – contains base64 encode file content using ToBase64String() method which accepts $fileContentsInBytes parameter and convert file to base64.

Using Set-Content cmdlet, it out base64 encoded file content to file at the specified location.

Cool Tip: How to find large size files in PowerShell!

PowerShell Base64 Encode String

Let’s understand with example to base64 encode string in PowerShell.

For example, if you want to base64 encode string, use below script

$StringMsg = "PowerShell Base64 Encode Example"

# Gets the bytes of String
$StringBytes = [System.Text.Encoding]::Unicode.GetBytes($StringMsg)

# Encode string content to Base64 string
$EncodedString =[Convert]::ToBase64String($StringBytes)

Write-Host "Encode String: " $EncodedString

In the above PowerShell script to convert string to base64,

$StringMsg – variable contains string content

$StringBytes – variable contains Bytes for a given string using Unicode.GetBytes()

Using the ToBase64String() method, it converts bytes to Base64 encoded string and prints it on the console.

You can use FromBase64String() method to convert from base64 encoded string.

$DecodedString = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($EncodedString))
Write-Output $DecodedString

In the above PowerShell script to decode base64 string,

It uses [System. Convert]:: FromBase64String method which accepts base64 encode the string as an input parameter.

Using [System.Text.Encode]::Unicode.GetString() method convert from base64 string in PowerShell.

The output of the above command is:

PowerShell base64 encode
PowerShell base64 encode

PowerShell Base64 Encode Image

You can easily convert image to base64 encode string as below

[convert]::ToBase64String((Get-Content D:\Logs-FTP01\powershell-mkdir.png -Encoding byte)) 

In the above script, the PowerShell Base64 Encode, it converts the image to the Base64 string.

PowerShell Get-Content cmdlet read the image file contents and convert the image to base64 string.

Cool Tip: How to use net user command in command prompt!

PowerShell Base64 Encode zip file

If you want to base64 encode zip file, use below PowerShell script

$zipfile = "D:\Logs-FTP01\EncodeLogic.zip"

$zipfilebytes = Get-Content $zipfile -Encoding byte
$zipfileBytesBase64 = [System.Convert]::ToBase64String($zipfilebytes)
$zipfileBytesBase64 | Out-File 'D:\Logs-FTP01\EncodeLogic.txt'

In the above PowerShell script,

$zipFile – contains zip file path

$zipfilebytes – contains the bytes information, Get-Content cmdlet read zip file content

$zipfileBytesBase64 – variable contains Base64 encode string for the zip file.

Base64 encodes zip file contents out to a .txt file as path specified.

Cool Tip: How to list files in a directory in PowerShell!


In the above article, I have explained about PowerShell base64 encode for image, string, file content or base64 encode zip file.

Using [System.Convert].ToBase64String in PowerShell, you can convert file to base64,convert string to base64 encode string.

You can read more about using PowerShell mkdir to create a new directory or New-Item cmdlet to create a directory if not exists and pass multiple parameters to function to create a directory.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment