How to Convert Files to Base64 in PowerShell?

Recently, while working on an automation project, I got a requirement to encode files to Base64, a process that converts binary data into ASCII string format. I will show you here different methods to convert files to Base64 using PowerShell with examples and complete scripts.

To convert a file to Base64 in PowerShell, you can use the [System.Convert]::ToBase64String method to encode a byte array obtained from the file. For example, $base64String = [System.Convert]::ToBase64String((Get-Content -Path ‘C:\MyFolder’ -Encoding Byte)) will give you the Base64 encoded string of the file, which you can then output or use as needed.

What is Base64 Encoding?

Base64 is a binary-to-text encoding mechanism that converts binary data in an ASCII string format into a radix-64 representation. This encoded data can then be easily transmitted or stored in text-based systems. This encoding is often used when transferring data over a medium that only supports text, such as embedding binary files within scripts or sending files via email.

Convert Files to Base64 Using System.Convert Class

To convert a file to Base64 in PowerShell, you can use the .NET System.Convert class. This class provides a ToBase64String method that can be used to convert a byte array into a Base64 string.

Here’s an example script that reads the content of a file into a byte array and then converts it to a Base64 string:

$fileContent = Get-Content -Path 'C:\MyFolder\Myexe.exe -Encoding Byte
$base64String = [System.Convert]::ToBase64String($fileContent)
Set-Content -Path 'C:\MyFolder\base64.txt' -Value $base64String

In this script, Get-Content reads the file as a byte array, which is then passed to ToBase64String to perform the conversion. The resulting Base64 string is saved to a text file using Set-Content.

Convert Files to Base64 Using Base64 Encoding with Streams

For large files, it’s more efficient to use streams to avoid loading the entire file into memory. The following example demonstrates how to use .NET streams to convert a file to Base64 using PowerShell:

$filePath = 'C:\MyFolder\Myexe.exe'
$outputPath = 'C:\MyFolder\base64.txt'

$inFileStream = [System.IO.File]::OpenRead($filePath)
$base64Stream = [System.IO.MemoryStream]::new()
$base64Writer = [System.IO.StreamWriter]::new($base64Stream)

$buffer = New-Object byte[] 8192
while ($inFileStream.Read($buffer, 0, $buffer.Length)) {
    $base64Writer.Write([System.Convert]::ToBase64String($buffer))
}

$base64Writer.Flush()
$base64Stream.Position = 0
$base64String = [System.IO.StreamReader]::new($base64Stream).ReadToEnd()

Set-Content -Path $outputPath -Value $base64String

$inFileStream.Close()
$base64Stream.Close()

This script creates a file stream for reading the input file and a memory stream for writing the Base64 encoded data. It reads chunks of the file into a buffer and writes the Base64 encoded string to the memory stream, which is then written to the output file.

You can see the output in the screenshot below after I executed the PowerShell script using VS code.

Convert Files to Base64 in PowerShell

Using a PowerShell Custom Function

To make the process reusable, you can create a custom PowerShell function that encapsulates the logic for converting a file to Base64.

Here is the complete PowerShell function.

function ConvertTo-Base64 {
    param (
        [Parameter(Mandatory=$true)]
        [string]$InputFile,
        [Parameter(Mandatory=$true)]
        [string]$OutputFile
    )

    $fileContent = Get-Content -Path $InputFile -Encoding Byte
    $base64String = [System.Convert]::ToBase64String($fileContent)
    Set-Content -Path $OutputFile -Value $base64String
}

# Usage
ConvertTo-Base64 -InputFile 'C:\MyFolder\file.exe' -OutputFile 'C:\MyFolder\base64.txt'

This function, ConvertTo-Base64, takes two parameters: the input file path and the output file path. It reads the content of the input file, encodes it to Base64, and saves it to the output file.

Conclusion

Encoding files to Base64 using PowerShell is easy by using the .NET System.Convert class.

I have also explained how to convert files to base64 using base64 encoding with Streams using PowerShell. I hope it helps.

You may also like the following PowerShell tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.